Bravat Posted February 14, 2011 Share Posted February 14, 2011 I am wondering about the following problem: I have two sessions, one for the user ($_SESSION['user_id']) and second one for the products inside cart ($_SESSION['cart']). I need to insert data into table racun. I did it this way and it works, but i want to know if this can be done better? while ($row=mysql_fetch_array($query)){ $id = $_SESSION['korisnik_id']; $idp = $row['product_id']; $kol = $_SESSION['cart'][$row['product_id']]['quantity'] ."<br>"; $insert = "INSERT INTO racun (product_id, quantity, korisnik_id) VALUES ('$idp', '$kol', '$id') "; $result = mysql_query($insert); } Link to comment https://forums.phpfreaks.com/topic/227666-while-insert-into/ Share on other sites More sharing options...
Pikachu2000 Posted February 14, 2011 Share Posted February 14, 2011 Any time you can avoid running a query in a loop, you should. In this case you could build the query string in a loop, then execute one INSERT query, using this syntax: INSERT INTO `table` (`field1`, `field2`) VALUES ( 'set1_value1', 'set1_value2'), ('set2_value1', 'set2_value2'), ('set3_value1', 'set3_value2'), etc. Link to comment https://forums.phpfreaks.com/topic/227666-while-insert-into/#findComment-1174293 Share on other sites More sharing options...
Bravat Posted February 14, 2011 Author Share Posted February 14, 2011 Thank you Link to comment https://forums.phpfreaks.com/topic/227666-while-insert-into/#findComment-1174296 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.