Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Insert Multiple Row from one form



5:09 pm on Mar 21, 2012 (gmt 0)

5+ Year Member

I have spent most of the day trying to sort this before asking for help but it's time to admit defeat.

I've got a form which using javascript adds more rows when needed. Here is how it looks:

<td><input type="text" class="w200" id="grade" name="grade[0]" value="" /></td>
<td><input type="text" class="w100" id="weight" name="weight[0]" value="" /></td>
<td><input type="text" class="w100" id="price" name="price[0]" value="" /></td>

What I'm trying to achieve is for each row on the form insert into the database on as a new row. Here's the insert script I've been messing around with:

foreach ($_POST as $key => $value) {
$sql1 = "INSERT INTO balance (balance_grade, balance_weight, balance_price) VALUES ('$value[grade]', '$value[weight]', '$value[price]')";
$result1 = mysql_query($sql1) or die(mysql_error());


It does insert data into the database but not what I'm looking for. If I do print_r ($_POST); I get the following results:

Array ( [date] => 2012-03-21 [date_dp] => 1 [date_year_start] => 2011 [date_year_end] => 2020 [date_mon] => [date_da1] => [date_da2] => [date_sna] => [date_aut] => [date_frm] => [date_tar] => [date_inp] => [date_fmt] => j F Y [date_dis] => [date_pr1] => [date_pr2] => [date_prv] => [date_pth] => ../calendar/ [date_spd] => [[],[],[]] [date_spt] => 0 [date_och] => [submit] => Submit [grade] => Array ( [0] => a ) [weight] => Array ( [0] => 1 ) [price] => Array ( [0] => 2 ) )

Grade = a, Weight = 1, Price = 2 are the values I typed into the form so it's partly working.

All the date stuff is from something else in the form which I'm not really interested in, only the [date] part of.

Any help would be greatly received, thanks.


10:25 am on Mar 22, 2012 (gmt 0)

5+ Year Member

I've solved it, here's the working code:

foreach($_POST['grade'] as $key => $grade) {
$weight = $_POST['weight'][$key];
$price = $_POST['price'][$key];
$sql1 = "INSERT INTO invoice_items (item_inv_id, item_grade, item_weight, item_price) VALUES ('$lastid', '$grade', '$weight', '$price')";
$result1 = mysql_query($sql1) or die(mysql_error());


11:24 am on Apr 28, 2012 (gmt 0)

Thank you for this code. I had the same form as op with javascript to add more field. The code works but I had to add an iteration because mysql would post all my defined value even though I only used less than my max value set. So I had to add.

if ($var1 && $var2 && $var3) {


3:17 pm on Apr 28, 2012 (gmt 0)

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Tip: if you want your print_r()'s to be nicely formatted and easy to read, wrap them in a <pre> tag like so:

echo '<pre>';
echo '</pre>';

I use it so often I made it a function I add in my common include file:

function pre_print_r($var) {
echo '<pre>';
echo '</pre>';

Featured Threads

Hot Threads This Week

Hot Threads This Month