homepage Welcome to WebmasterWorld Guest from 54.211.47.170
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Insert Multiple Row from one form
LinusIT




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

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:

if(isset($_POST['submit'])){
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.

 

LinusIT




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

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());
}

duceduc




msg:4446838
 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) {
}

incrediBILL




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

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>';
print_r($_POST);
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>';
print_r($var);
echo '</pre>';
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved