Diether Posted March 17, 2013 Share Posted March 17, 2013 Hi guys Good day, how to insert multiple data in the database? when i insert data that has multiple orders, only one item is inserted in the database. pls help me to put this on loop.Heres the code i use: foreach ($_SESSION["cart_array"] as $each_items){ $item_id = $each_items['item_id']; $quantity = $each_items['quantity'] ; $sql = mysql_query("SELECT * FROM product WHERE id = '$item_id'"); while($row = mysql_fetch_array($sql)){ $product_name = $row['name']; $price = $row['price']; $total_price = $price * $quantity; mysql_query("INSERT INTO customer_order( id,quantity,item_id, total_price,shipping_address, shipping_date,customer_id) VALUES ('','$quantity','$item_id','$total_price', '','', '$lastId')") or die (mysql_error()); } } Heres what i did but it gives me syntax error, pls help me to do the proper looping for this. thanks foreach ($_SESSION["cart_array"] as $each_items){ $item_id = $each_items['item_id']; $item_id_count = count($item_id) ; $quantity = $each_items['quantity'] ; $sql = mysql_query("SELECT * FROM product WHERE id = '$item_id'"); while($row = mysql_fetch_array($sql)){ $product_name = $row['name']; $price = $row['price']; $total_price = $price * $quantity; foreach($i=0,$i < $item_id_count,$i++){ mysql_query("INSERT INTO customer_order( id,quantity,item_id, total_price,shipping_address, shipping_date,customer_id) VALUES ('','$quantity','$item_id','$total_price', '','', '$lastId')") or die (mysql_error()); } } } Quote Link to comment https://forums.phpfreaks.com/topic/275770-how-to-insert-multiple-data-in-the-table/ Share on other sites More sharing options...
subhomoy Posted March 17, 2013 Share Posted March 17, 2013 (edited) you have left two blank spaces (i.e shipping_date & customer_id).. Check ur SQL query VALUES ('','$quantity','$item_id','$total_price', '','', either fill it up with the details or do this one to add more specifically mysql_query("INSERT INTO customer_order(id,quantity,item_id,total_price,customer_id)VALUES('','$quantity','$item_id','$total_price', '$lastId')") or die (mysql_error()); Edited March 17, 2013 by subhomoy Quote Link to comment https://forums.phpfreaks.com/topic/275770-how-to-insert-multiple-data-in-the-table/#findComment-1419163 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.