Reputation: 3514
I have a script where I am getting shopping cart IDs and then lowering the availability count based on if the product has been paid. Each cart might contain 1 or 10 products, so it's got to happen for each of the items. When someone pays (confirmed via PayPal IPN), I am running:
// 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'] . "'");
}
}
//
}
However, nothing seems to happen and neither am I getting an error message. What am I doing wrong here?
Upvotes: 1
Views: 142
Reputation: 91742
This looks wrong to me:
// 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))
Assuming mysqli, you would need something like:
// 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())
Note that you seem to be using fetch_array()
on the wrong object and you are passing it an invalid parameter.
To make sure you see any errors you need to make sure that you are displaying them and you should check for them, for example by telling mysqli to throw exceptions. To do that, at this to the top of your script:
ini_set('display_errors',1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Upvotes: 2