Jump to content

How do I insert multiple new items into database??


Scooby08

Recommended Posts

Say I have a form like so:

 

<fieldset>
<label>Description</label><br />
<input type="text" name="description[]" value="" /><br />
<label>Qty</label><br />
<input type="text" name="qty[]" value="" /><br />
<label>Price</label><br />
<input type="text" name="price[]" value="" /><br />
</fieldset>

 

The user can click to add as many new items as they wish.. How do I collect all that information so that each item is inserted into a new row in the database?? It's simple to do this when only one item is added at a time, but in this case they can add multiple items at once and that's where I'm stumped.. Can anybody point me in the right direction??

Using a loop. You would need to use multiple queries as well. Unless you can do it all in one query, my knowledge of MySQL is limited.

 

$desc = $_POST['description'];
$qty = $_POST['qty'];
$price = $_POST['price'];

//all items will be corresponding in arrays, so $desc[0] will have a qty of $qty[0]
for($i = 0; $i < count($desc); $i++){
//insert query here
}

Thanks for the replies guys.. I tried ProjectFear's solution and came up with this..

 

<?php
$item_descr_aai = $_POST['item_descr_aai'];
$item_qty_aai = $_POST['item_qty_aai'];
$item_price_aai = $_POST['item_price_aai'];

for($i = 0; $i < count($item_descr_aai); $i++){
$query_order_items_aai  = "INSERT INTO dw_order_items (item_descr, item_qty, item_price) "; 
$query_order_items_aai .= "VALUES ($item_descr_aai, '$item_qty_aai', '$item_price_aai') ";
echo $query_order_items_aai;
}
?>

 

This is what it echo's:

 

INSERT INTO dw_order_items (order_id, item_id, item_qty) VALUES (Array, 'Array', 'Array')

 

And for Xurion's solution how would that foreach collect all the info and place it in order in the database?

In this context, I would prefer the for loop.

 

$item_descr_aai = $_POST['item_descr_aai'];
$item_qty_aai = $_POST['item_qty_aai'];
$item_price_aai = $_POST['item_price_aai'];

for($i = 0; $i < count($item_descr_aai); $i++){
//start looping
$query_order_items_aai = "INSERT INTO dw_order_items (item_descr, item_qty, item_price) ";
$query_order_items_aai .= "VALUES ('{$item_descr_aai[$i]}','{$item_qty_aai[$i]}','{$item_price_aai[$i]}')";

echo $query_order_items_aai;
}

 

Hope this helps.

Ahh nice!! It's echoing all info but one.. the qty.. But I forgot to mention something about that one.. In the field input name I also have a value assigned to the array name.. Here is what the qty input is like:

 

<input type="text" name="qty[item_1]" value="" />

 

if the user wants to add another item then it would be like so (along with the rest of the form):

 

<input type="text" name="qty[item_1]" value="" />
<input type="text" name="qty[item_2]" value="" />

 

item_1 and item_2 is what I'm using as the id for that specific item..  How do I get that into this equation?? I need to enter item_1, item_2 into the database as well like so..

 

<?php
$query_order_items_aai  = "INSERT INTO dw_order_items (item_id, item_descr, item_qty, item_price) ";
$query_order_items_aai .= "VALUES ('{$item_id[$i]}','{$item_descr_aai[$i]}','{$item_qty_aai[$i]}','{$item_price_aai[$i]}')";
?>

I'm having trouble thinking. ;)

 

Use print_r() to see what the qty field is returning after submitting the form.

 

$item_descr_aai = $_POST['item_descr_aai'];
$item_qty_aai = $_POST['item_qty_aai'];
$item_price_aai = $_POST['item_price_aai'];

echo "<pre>";
print_r($item_qty_aai);
echo "</pre>";

exit;

 

Copy and paste what that displays.

I'm sorry.. I don't understand how that value gets entered into the database under item_id field??

 

$query_order_items_aai  = "INSERT INTO dw_order_items (item_id, item_descr, item_qty, item_price) ";
$query_order_items_aai .= "VALUES ('{$item_descr_aai[$i]}','".$item_qty_aai['add_item_'.($i+1)]."','{$item_price_aai[$i]}')";

 

see here: INSERT INTO dw_order_items (item_id

 

it needs to be that value.. Is the way you just told me able to do that and Im just missing the point? It looks as if this ['add_item_'.($i+1)] is supposed to equal add_item_1 and just add 1 for each loop.. The thing is that this form is created as the user clicks to add an item and I already add 1 to it then..

Here's what I got working properly..

 

<?php
$item_id = array_keys($_POST['item_qty_aai']);
$item_descr_aai = $_POST['item_descr_aai'];
$item_qty_aai = array_values($_POST['item_qty_aai']);
$item_price_aai = $_POST['item_price_aai'];

for($i = 0; $i < count($item_descr_aai); $i++){
$query_order_items_aai  = "INSERT INTO dw_order_items (item_id, item_descr, item_qty, item_price) ";
$query_order_items_aai .= "VALUES ('{$item_id[$i]}','{$item_descr_aai[$i]}','{$item_qty_aai[$i]}','{$item_price_aai[$i]}')";
mysql_query($query_order_items_aai) or die(mysql_error());
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.