简体   繁体   中英

How Can I subtract number from mysql table to finish in php

I have table named tb_product with three columns ProductID, Quantity and Price if for example Product with ID 5 has a quantity of 20 and somebody bought one item out of it it should be remaining 19 items from the table. That is anytime an order is made product quantity should be subtracted by the number ordered till the product reaches 0 or finishes from the product table. I have no idea about the it, please somebody help me.

update tb_product set quantity=quantity-1 where productid=productid

My question was partially answered by Raphul Chauham, because his answer does not give me exactly what i was looking for but subtracted the quantity column by 1 instead of by the variable representative from the order. Below is what I wanted exactly.

<?php
$updateProduct=mysql_query("UPDATE tb_products SET quantity=quantity-'$quantity' WHERE productID='$product_id' and quantity>0");
?>

The answer I considered favorable was only subtracting quantity by one whether order quantity is 20 but with this, the subtraction vary as user order varies by the variable $quantity

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.

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