我有一个脚本,我可以在其中获取购物车 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);
关于php - while/foreach 循环更新 mySQL 中的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27537713/