Scooby08 Posted August 18, 2008 Share Posted August 18, 2008 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?? Quote Link to comment https://forums.phpfreaks.com/topic/120156-how-do-i-insert-multiple-new-items-into-database/ Share on other sites More sharing options...
JasonLewis Posted August 18, 2008 Share Posted August 18, 2008 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 } Quote Link to comment https://forums.phpfreaks.com/topic/120156-how-do-i-insert-multiple-new-items-into-database/#findComment-619039 Share on other sites More sharing options...
Xurion Posted August 18, 2008 Share Posted August 18, 2008 I prefer the foreach loop when dealing with arrays: foreach($_POST['description'] as $value){ echo $value.'<br>'; } This echos the value of each. Quote Link to comment https://forums.phpfreaks.com/topic/120156-how-do-i-insert-multiple-new-items-into-database/#findComment-619040 Share on other sites More sharing options...
Scooby08 Posted August 18, 2008 Author Share Posted August 18, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/120156-how-do-i-insert-multiple-new-items-into-database/#findComment-619043 Share on other sites More sharing options...
JasonLewis Posted August 18, 2008 Share Posted August 18, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/120156-how-do-i-insert-multiple-new-items-into-database/#findComment-619044 Share on other sites More sharing options...
Scooby08 Posted August 18, 2008 Author Share Posted August 18, 2008 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]}')"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/120156-how-do-i-insert-multiple-new-items-into-database/#findComment-619047 Share on other sites More sharing options...
JasonLewis Posted August 18, 2008 Share Posted August 18, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/120156-how-do-i-insert-multiple-new-items-into-database/#findComment-619050 Share on other sites More sharing options...
Scooby08 Posted August 18, 2008 Author Share Posted August 18, 2008 I ran that code and here is what it outputs: Array ( [add_item_1] => 3 [add_item_2] => 4 [add_item_3] => 5 ) I added 3 items then submitted Quote Link to comment https://forums.phpfreaks.com/topic/120156-how-do-i-insert-multiple-new-items-into-database/#findComment-619052 Share on other sites More sharing options...
JasonLewis Posted August 18, 2008 Share Posted August 18, 2008 Okay well if it starts at 1, then you would do it like this: $query_order_items_aai .= "VALUES ('{$item_descr_aai[$i]}','".$item_qty_aai['add_item_'.($i+1)]."','{$item_price_aai[$i]}')"; Quote Link to comment https://forums.phpfreaks.com/topic/120156-how-do-i-insert-multiple-new-items-into-database/#findComment-619055 Share on other sites More sharing options...
Scooby08 Posted August 18, 2008 Author Share Posted August 18, 2008 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.. Quote Link to comment https://forums.phpfreaks.com/topic/120156-how-do-i-insert-multiple-new-items-into-database/#findComment-619057 Share on other sites More sharing options...
Scooby08 Posted August 18, 2008 Author Share Posted August 18, 2008 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()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/120156-how-do-i-insert-multiple-new-items-into-database/#findComment-619066 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.