HELP! Need to insert multiple rows into a database table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ladieballer2004
Forum Newbie
Posts: 3
Joined: Wed Jun 10, 2009 3:32 am

HELP! Need to insert multiple rows into a database table

Post by ladieballer2004 »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Ok so I have built this form and it contains 3 rows with the same information asked. The user should have the option to fill out one, two, or all three rows.
Upon submission I want each row of form data to be entered into seperate database table rows. Myd DB is MYSQL 5.1.33 and i'm using php5

Here is my form

Code: Select all

<form  action="insert.php"  method="post" >
<td height="63" colspan="5"><h3>Other involvement during high school, college (clubs, sports, work, volunteer, etc.): </h3></td>
    </tr>
<td width="13%" height="60"><h3>#</h3></td>
      <td width="20%"><h3>Activity</h3></td>
      <td width="19%"><h3>Position</h3></td>
      <td width="23%"><h3>Start Date</h3></td>
      <td width="25%" height="60"><h3>End Date</h3></td>
    </tr>
    <tr>
      <td height="63"><label for="InvolvementID"></label>
      <input name="ID1" type="text" id="ID1" size="6" /></td>
      <td height="63"><label for="Activity1"></label>
      <input name="Activity1" type="text" id="Activity1" size="15" /></td>
      <td height="63"><label for="Position1"></label>
      <input name="Position1" type="text" id="Position1" size="15" /></td>
      <td height="63"><label for="StartDate1"></label>
      <input name="StartDate1" type="text" id="StartDate1" size="15" /></td>
      <td height="63"><label for="EndDate1"></label>
      <input name="EndDate1" type="text" id="EndDate1" size="15" /></td>
    </tr>
    <tr>
      <td height="63"><input name="ID2" type="text" id="ID2" size="6" /></td>
      <td height="63"><input name="Activity2" type="text" id="Activity2" size="15" /></td>
      <td height="63"><input name="Position2" type="text" id="Position2" size="15" /></td>
      <td height="63"><input name="StartDate2" type="text" id="StartDate2" size="15" /></td>
      <td height="63"><input name="EndDate2" type="text" id="EndDate2" size="15" /></td>
    </tr>
    <tr>
      <td height="62"><input name="ID3" type="text" id="ID3" size="6" /></td>
      <td height="62"><input name="Activity3" type="text" id="Activity3" size="15" /></td>
      <td height="62"><input name="Position3" type="text" id="Position3" size="15" /></td>
      <td height="62"><input name="StartDate3" type="text" id="StartDate size=" size="15"15" /></td>
      <td height="62"><input name="EndDate3" type="text" id="EndDate3" size="15" /></td>
</form>
my real problem comes my insert.php document. This is what I have for the insert query and i'm positive that it's wrong. PLEASE HELP

Code: Select all

$involv= foreach ($_POST['ID']) ) 
{mysql_query("INSERT INTO Involvement (ID,Activity, Position. StartDate, EndDate)
            VALUES ('$_POST[ID];''$_POST[Activity];','$_POST[Position];','$_POST[StartDate];','$_POST[EndDate];')";

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: HELP! Need to insert multiple rows into a database table

Post by mikemike »

There's a few problems here. I dont have time to go through them all in detail unfortunately. Have a read of some tutorials on escaping strings and read the foreach documentation. Also, escape $_POST['ID'] before purring it into the query.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: HELP! Need to insert multiple rows into a database table

Post by pickle »

You're looping through $_POST['ID'] but nowhere do you set that variable up as an array. Rather than:

Code: Select all

<input name="ID1" type="text" id="ID1" size="6" />
do this:

Code: Select all

<input name="ID[1]" type="text" id="ID1" size="6" />
Then $_POST['ID'] will actually be an array. Set the rest of your variables up like that & treat them like arrays.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply