库存超出限制,仍然插入订单表

时间:2013-09-29 03:30:09

标签: php mysql sql

$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.!');
}

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

1 个答案:

答案 0 :(得分: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