While I am coding a trading site, I need to update the product stock. But the fact is that, of course, a shopping basket can have the same items a couple of times. What is the best way to update it?
I tried IN , but the following SQL query returns 3 elements.
SELECT * FROM `products` WHERE id IN ( 3, 4, 4, 6 ) LIMIT 0 , 30
Here is my solution, but I don’t think it is the best.
$cart = array(1,3,4,4,5,8,22,22); $itemlist = array_count_values($cart); foreach($itemlist as $itemid=>$ocurrence){ $SQL = "UPDATE products SET stock = stock-".$ocurrence." WHERE id = ".$itemid; mysql_query($SQL); }
source share