1
$prodqty = mysql_query("SELECT quan FROM pro_list WHERE auto_id = $pid"); //get the current product quantity
if (mysql_num_rows($prodqty) != 0)
{
    $row = mysql_fetch_array($prodqty);
    $productqty = $row['quan'];
}
$nqty = $productqty-$q; //current product quantity minus order quantity to get new product quantity

if ($nqty >= 0)
{  
    $query2="UPDATE pro_list SET quan = $nqty WHERE auto_id = $pid"; //update the quantity in the product table
    $result = mysql_query($query2);

    if ($result)
      echo "Successfully ";
    else
      echo "Unsuccesfully";
}
else
    echo "Limit of quantity! .";
}
die('Thank You For Shopping With i-Supply System! your order has been sent to Admin.!');
}

库存超出限制,但订单仍插入数据库。该订单不应插入到表订单中,因为它已经超出了产品数量的限制。客户应再次下订单。为什么会发生这种情况?

4

1 回答 1

2

你的方法从根本上是有缺陷的。如果同时尝试两个或多个更新,您将获得竞争条件。您应该将检查和更新作为单个查询执行,如下所示:

$query="UPDATE pro_list SET quan=quan-$q WHERE auto_id=$pid and quan>=$q";

$result = mysql_query($query) or die(mysql_error());

if (mysql_affected_rows()  == 0) {
  echo "Out of stock!";
} 

在任何情况下,您都不应该使用mysql- 它已被弃用。使用mysqliPDO代替。

于 2013-09-29T03:48:17.180 回答