Jump to content

while insert into


Bravat

Recommended Posts

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

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

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.