简体   繁体   English

在php中签出购物车时如何减少商品数量?

[英]How to reduce item quantity when checking out a shopping cart in php?

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? 我正在做一个购物车系统,但不确定在php中签出购物车时如何减少mysql中的项目数量?

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. 例如,当购买了第2个item1时,mysql中的数量列应减少2。即,相对于所购买的数量,应减少数量。

You can run a simple MySQL UPDATE query: 您可以运行一个简单的MySQL UPDATE查询:

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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM