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); } Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Bravat Posted February 14, 2011 Author Share Posted February 14, 2011 Thank you Quote Link to comment 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.