而 / foreach 循环更新 mySQL 中的数量


while / foreach loop updating quantity in mySQL

我有一个脚本,我在其中获取购物车 ID,然后根据产品是否已付款降低可用性计数。每个购物车可能包含 1 或 10 个产品,因此每个项目都必须发生。当有人付款(通过PayPal IPN确认(时,我正在运行:

// we set the cart id, 12345 in this case
$cart_id = '12345'
// we get the buyer ID
$buyer_id = $db->get_sql_field("SELECT buyer_id FROM db_carts WHERE sc_id='" . $cart_id . "'", "buyer_id");
// we get the products.
$sql_select_cart = $db->query("SELECT item_id, quantity FROM db_shopping_carts_items WHERE sc_id='" . $cart_id . "'");
while ($cart_details = $db->fetch_array($sql_select_cart))
        {
            //
            foreach ($cart_details as $key => $item_details)
            {
                if ($item_details['quantity']<='100')
                {
                    $db->query("UPDATE db_products SET quantity=quantity-" . $item_details['quantity'] . 
                    " WHERE product_id='" . $item_details['item_id'] . "'");
                }
            }
            //
        }

但是,似乎什么也没发生,也没有收到错误消息。我在这里做错了什么?

对我来说看起来不对:

// we get the products.
$sql_select_cart = $db->query("SELECT item_id, quantity FROM db_shopping_carts_items WHERE sc_id='" . $cart_id . "'");
while ($cart_details = $db->fetch_array($sql_select_cart))

假设 mysqli,您将需要类似的东西:

// we get the products.
$sql_select_cart = $db->query("SELECT item_id, quantity FROM db_shopping_carts_items WHERE sc_id='" . $cart_id . "'");
while ($cart_details = $sql_select_cart->fetch_array())

请注意,您似乎在错误的对象上使用fetch_array(),并且您正在向其传递无效参数。

为了确保您看到任何错误,您需要确保显示它们并检查它们,例如通过告诉 mysqli 抛出异常。为此,请在此处到脚本的顶部:

ini_set('display_errors',1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);