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


The stock was out of limit, still insert into order table

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

库存超出限制,但订单仍会插入到数据库中。订单不应插入到表订单中,因为它已经超出了产品数量的限制。客户应重新下订单。为什么会这样?

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

$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