I'm doing a shopping cart system and I'm not sure how to reduce item quantity in mysql when checking out a shopping cart in php?
For example, when 2 of item1 are purchased, the quantity column in mysql should be reduced by 2. ie the quantity should be reduced with respect to the quantity purchased.
You can run a simple MySQL UPDATE
query:
UPDATE `products` SET `quantity` = `quantity` - num_purchased WHERE `id` = 15
Obviously you'll need to replace the values, field names and table names with those you actually use...
See If you multiple products in cart, you should loop through product like below
foreach ($cartItem as $cart) {
$productId = $cart['product_id'];
$qty = $cart['qty'];
$sql = "UPDATE `products` SET `num_of_stocks` = `num_of_stocks` - $qty
WHERE `id` = $productId";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.