更新查询中的Echo

时间:2014-05-26 16:41:50

标签: php mysql

有人可以告诉我,在此查询中,如果数量超过库存,如何进行回音?

$updatequery =
    "UPDATE products SET
        stock =
            CASE WHEN stock >= '$quantity' THEN
                (stock-'$quantity')
            ELSE
                stock
            END
    WHERE product = '$product' " ;

我试过这样的东西,但它不起作用!

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

1 个答案:

答案 0 :(得分:-1)

试试这个:

$updatequery = "
    UPDATE `products`
    SET `stock` = `stock` - ".intval($quantity)."
    WHERE `product` = '".appropriate_escaping_function($product)."'
      AND `stock` >= ".intval($quantity)."
";

然后,检查affected_rows函数(如果不知道您正在使用哪个库,则无法确切地告诉您哪个函数,但快速搜索应该找到它)。如果它为零,则表示未找到产品(即使在尝试更新之前您应该已经知道),或者stock >= quantity检查失败。