PHP not inserting multiple records correctly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rswconnect
    New Member
    • Feb 2013
    • 3

    PHP not inserting multiple records correctly

    HELP!

    I've looked through many posts asking this same question and followed several tutorials for inputting multiple records using one INSERT, but can't seem to get this to work.

    The result is the only the second record being entered into the database, twice, being signed consecutive auto increment bowling_id numbers.

    The form:

    Code:
    <form action="bowlingscoresprocess.php" method="post">
    <table width="600" border="2" cellspacing="2" cellpadding="2">
    <tr>
    <th scope="col">ID</th>
    <th scope="col">Date</th>
    <th scope="col">Name</th>
    <th scope="col">Game 1</th>
    <th scope="col">Game 2</th>
    </tr>
     
    <tr>
    <td><input type="text" name="bowlscores_id" value=""></td>
    <td><input type="text" name="bowldate" value="Thursday, Mar 1, 2013"></td>
    <td><input type="text" name="bowllastfirst" value="Smith, Joe"></td>
    <input type="hidden" name="bowlfirstlast" value="Joe Smith">
    <td><input type"text" name="bowlgamea"></td>
    <td><input type="text" name="bowlgameb"></td>
    </tr>
    
    <tr>
    <td><input type="text" name="bowlscores_id" value=""></td>
    <td><input type="text" name="bowldate" value="Thursday, Mar 1, 2013"></td>
    <td><input type="text" name="bowllastfirst" value="Johnson, Sam"></td>
    <input type="hidden" name="bowlfirstlast" value="Sam Johnson">
    <td><input type"text" name="bowlgamea"></td>
    <td><input type="text" name="bowlgameb"></td>
    </tr>
    
    </table>
    
    <center>
    <table border="0" cellspacing="1" cellpadding="3" 
            align="center">
      	<tr>
        <td  colspan=2 align="center">
        <input type="submit" name="Submit" value="Submit">
        </td>
          <td width="5">
        &nbsp;&nbsp;&nbsp;&nbsp;
        </td>
      	<td  colspan=2 align="center">
    	<input type="reset" name="reset" value="Reset "/> 
    	</td>  
     	</tr>
      	</table>

    The processing:

    Code:
    <?php
    $con = mysql_connect("localhost", "mydatabase", "password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("mydatabase", $con);
    
    $sql="INSERT INTO bowlingscores (bowlscores_id, bowldate, bowllastfirst, bowlfirstlast, bowlgamea, bowlgameb)
    VALUES
    ('$_POST[bowlscores_id]','$_POST[bowldate]','$_POST[bowllastfirst]','$_POST[bowlfirstlast]','$_POST[bowlgamea]','$_POST[bowlgameb]'),
    ('$_POST[bowlscores_id]','$_POST[bowldate]','$_POST[bowllastfirst]','$_POST[bowlfirstlast]','$_POST[bowlgamea]','$_POST[bowlgameb]')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
       }
    echo "Records added";
    
    mysql_close($con);
    ?>
    Thanks for any answers you can provide!
    Last edited by Rabbit; Feb 20 '13, 05:58 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    That's because on line 12 and 13, you're inserting the same values twice.

    Comment

    • rswconnect
      New Member
      • Feb 2013
      • 3

      #3
      Hi Rabbit,

      From what I thought I read: the reason I inserted VALUE inputs, separated by a comma, in the php is because there are two record inputs from the form to process.

      If not the right way . . . do you have an idea of what is?

      Thanks,
      rsw

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        the reason I inserted VALUE inputs, separated by a comma
        Yes, you can do this to insert more than one record. IF you provide different values to insert. But you're submitting the same value.

        Comment

        • rswconnect
          New Member
          • Feb 2013
          • 3

          #5
          Thanks for your help. Gonna' have to rework the DB.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            It's not so much the DB as the values you're passing to the page. You're passing the variables with the same name. In your case, the simplest solution is to pass the variables with different names so you can refer to the right record in your insert values.

            Comment

            Working...