View Full Version : Adding multiple records to a table in one form.

05-04-2006, 11:10 PM
Okay, was not quite sure what topic to post this on.

I have one form in a php page. In that form there are several rows, each with several input boxes. These rows represent individual products and their price. You may add as many products as you want by clicking a add product button and it will add another row using javascript. When you are finished adding your products you then click submit. For each product I would like it to insert an individual record in the products table.

For example if you add 10 products, when you click submit it will add 10 records in the product table.

Can you give me some direction in the matter.


05-05-2006, 03:32 AM
so in your form you would have some kind of place holder so you know how to identify each row right? say rowid or whatever?

you would then do one of two things, either create an array that holds all of your values and then in your sql you would step through the array to insert each row OR you would create a loop and do a single sql insert for each row.

05-05-2006, 03:54 PM
You need to give the textbox the Record ID, or something that will uniquely identify it.

Because HTML requires a letter to start a name or ID then you will have to name your boxes accordingly and then split the name after submit.

<form name="form1" method="post" action="">
<input name="id_1" type="text" id="id_1" value="30">
<input name="id_23" type="text" id="id_23" value="50">
<input name="text" type="text" id="id_23" value="tezxt">
<input name="id_45" type="text" id="id_45" value="4">
<input type="submit" name="Submit" value="Submit">
foreach($_POST as $key => $val){
if(strpos($key, 'id_') !== false){;
$id = split('id_', $key);
//echo $id[1] . ': ' . $val . '<br>';
$sql = "INSERT INTO table (id, amount) VALUES (" . $id[1] . "'," . $val . ")";
echo $sql . '<br>';