我正在拍賣網站上用戶可以出價並贏得拍賣。在我的情況下,拍賣結束時出價結束(管理員設置的出價數量)。所以我需要關於最終出價的建議。如果最終出價是由兩個不同的用戶完全同時放置會怎樣?需要在mysql中同時插入兩條記錄的建議
現在,當有人點擊出價按鈕,我發送請求到ajax並在那裏插入記錄。我知道要做到這一點的唯一方法是從數據庫中檢查並確定有多少出價並相應地插入(通過允許一個用戶),但是如果時間完全相同,則可能再次出現異常。同時,我不想浪費時間再次查詢數據庫,因爲它會延遲投標並可能產生更多麻煩。
這是我的PHP代碼,它使出價。
$cid = $_GET['cid'];
$uid = $_GET['uid'];
$pid = $_GET['pid'];
$type = $_GET['type'];
$date = date("Y-m-d H:i:s");
$placeBid = $obj->insert("bids",array("productid"=>$pid,"userid"=>$uid,"coinsid"=>$cid,"type"=>$type,"date"=>$date));
if($placeBid)
{
//if bid is successfull, update the status of coins in coins table.
$obj->update("coins","status=? where id=?",array(0,$cid));
//Also update bid counts in product table, to ensure the bids placed on specific product
if($type == 'paid')
{
$obj->update("products","bidscount=bidscount+1 where id=?",array($pid));
}
//check if still coins are left with user
//get bid coins for current user.
$bidCoins = $obj->select("coins","*","userid=? and status=?",array($userid,1));
if($bidCoins)
{
$coinsHtml = '<a href="#"class="close"/>X</a>
<div class="coins_popup">
<h3>Select your coins box and bid</h3>';
foreach((array)$bidCoins as $bidCoinsR)
{
$b = ($bidCoinsR['type'] == 'free')?"(B)":"";
if($bidCoinsR['type'] == 'free')
{
$type = 0;
}
else if($bidCoinsR['type'] == 'paid')
{
$type = 1;
}
$coinsHtml .= '<a href="javascript:void(0)" onclick="placeBid('.$bidCoinsR["id"].','.$bidCoinsR["userid"].','.$type.')"><span>'.$bidCoinsR["amount"].$b.'</span></a>';
}
$coinsHtml .= '<input type="hidden" id="product_id" value="" name="product_id">';
echo $coinsHtml .= '</div>';
}
else
{
echo 'You have no more coins available';
}
}
能否請你給我建議的最佳方式。謝謝
什麼版本的MySQL您使用的是?一個非常通用的建議是在插入出價時鎖定表格。這將消除您所描述的任何競爭條件的可能性。你有沒有讀過:http://dev.mysql.com/doc/refman/5.5/en/innodb-locking-reads.html – kasdega
嗯,以前沒有用過。我會考慮鎖定。謝謝:) –