2013-09-29 2 views
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.!'); 
} 

재고가 한계를 초과했지만 주문이 여전히 데이터베이스에 삽입됩니다. 주문량이 이미 제품 수량을 초과하여 주문서에 삽입해서는 안됩니다. 고객은 다시 주문해야합니다. 왜 이런 일이 일어날까요?재고가 여전히 제한되어 있고 주문 표에 여전히 삽입되어 있습니다.

+0

'$ q'를 보여주십시오. 실제로'$ nqty = $ productqty- $ q'는 보지 못합니다. – Imran

+0

$ nqty는 주문 후 제품 잔액이며 $ productqty는 주식의 실제 수량이며 $ q는 고객이 주문하는 동안의 수량입니다. @imran – ayeen

답변

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를 사용하지 않아야 - 그것은 사용되지 않는 것. 대신 mysqli 또는 PDO을 사용하십시오.

관련 문제