I would like to run an update query with an if statement on a MySQL database using PHP. I would like to update the stock in my eshop, but only if the quantity that customer order exists. I tried something like this
$updatequery = "UPDATE products
SET stock
CASE WHEN (stock >= '$quantity') THEN (stock=stock-'$quantity')
ELSE (stock= stock)
END
WHERE product='$product'"; `
or like this
$sql = "UPDATE products SET\n"
. "stock = IF(stock>=$quantity, stock=stock-$quantity, IF(stock<=$quantity, stock=stock) )\n"
. "WHERE product=$product"; `
Is there anyone that could help me with the issue I'm having?