Can someone tell me, in this query how to do the echo if the quantity is more than stock?
$updatequery =
"UPDATE products SET
stock =
CASE WHEN stock >= '$quantity' THEN
(stock-'$quantity')
ELSE
stock
END
WHERE product = '$product' " ;
I tried something like this but it doesn't work!
if (!$updatequery) {
echo " We are sorry! The available stock of selected product ($stock units) in not enough ";
} else {
echo "Hello $username! You ordered $product with quantity: $stock succesfully";
}
Try this:
$updatequery = "
UPDATE `products`
SET `stock` = `stock` - ".intval($quantity)."
WHERE `product` = '".appropriate_escaping_function($product)."'
AND `stock` >= ".intval($quantity)."
";
Then, check the affected_rows
function (can't tell you exactly what function without knowing what library you're using, but a quick search should find it). If it's zero, then either the product wasn't found (which you should already know before even trying to update) or it failed the stock >= quantity
check.