Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP and MYSQL with a loop

    Hey guys,

    I am trying to input data into a mysql database from data thats been added by a jquery form with cloned fields. It is working however it adds the first row correctly then a blank row and then the rest of the rows correctly

    The code is this

    $x=0;

    do {
    $fga=$_POST['ID'.$x.'fga'];
    $fgm=$_POST['ID'.$x.'fgm'];
    $fta=$_POST['ID'.$x.'fta'];
    $ftm=$_POST['ID'.$x.'ftm'];
    $tpa=$_POST['ID'.$x.'tpa'];
    $tpm=$_POST['ID'.$x.'tpm'];
    $fta=$_POST['ID'.$x.'fta'];
    $ftm=$_POST['ID'.$x.'ftm'];
    $stl=$_POST['ID'.$x.'stl'];
    $trb=$_POST['ID'.$x.'trb'];
    $ast=$_POST['ID'.$x.'ast'];
    $blka=$_POST['ID'.$x.'blka'];
    $blkf=$_POST['ID'.$x.'blkf'];
    $tovr=$_POST['ID'.$x.'tovr'];

    mysql_query("INSERT INTO stats (gameId, fga, fgm, tpa, tpm, fta, ftm, ast, stl, trb, blka, blkf, tovr) VALUES ('$gameId', '$fga', '$fgm', '$tpa', '$tpm', '$fta', '$ftm', '$ast', '$stl', '$trb', '$blka', '$blkf', '$tovr')")or die("<div class='errmsg'>Insert Error: ".mysql_error()."</div><br /> \n");

    $x++;

    Can anyone see what ive done wrong?

    Thanks

  • #2
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Do a little bug testing, try:

    PHP Code:
    PRINT "INSERT INTO stats (gameId, fga, fgm, tpa, tpm, fta, ftm, ast, stl, trb, blka, blkf, tovr) VALUES ('$gameId', '$fga', '$fgm', '$tpa', '$tpm', '$fta', '$ftm', '$ast', '$stl', '$trb', '$blka', '$blkf', '$tovr')")or die("<div class='errmsg'>Insert Error: ".mysql_error()."</div><br /> \n"
    Also check your error log. I am betting the form and $_POST values aren't matching.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    You've shown us a very limited bit of code there which isn't really enough for us to see whats going on.

    The only thing I can see is that you're using $x in the form field names to access the fields. THis suggests one of two things: Your second set of fields are not named correctly OR they are being sent with null values for some reason.

    You need to var_dump($_POST) to see what is being sent from the browser to php.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    You've shown us a very limited bit of code there which isn't really enough for us to see whats going on.

    The only thing I can see is that you're using $x in the form field names to access the fields. THis suggests one of two things: Your second set of fields are not named correctly OR they are being sent with null values for some reason.

    You need to var_dump($_POST) to see what is being sent from the browser to php.
    Thanks guys Ill try these and get back to you

  • #5
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by nyther View Post
    Thanks guys Ill try these and get back to you
    Turned out to be an issue where x was starting at 1 instead of 0 in the previous page oops!

    thanks for the help

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Thats not possible given that you were using this code:
    $x=0;

    do {

    Unless you showed us wrong code?
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    I think he means that he had the starting <form method="post"> fields named "ID1fga", "ID1fgm", etc.

    Whereas the PHP code was expecting to find fields named "ID0fga", "ID0fga", etc.

    So it all makes perfect sense.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Ah i see yes that would make sense..
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •