我在拍卖网站上工作,用户可以出价,赢得拍卖。在我的情况下,拍卖结束时,招标结束(从管理设置的出价数量)。所以我需要关于最终出价的建议。如果最终的出价是由2个不同的用户在完全相同的时间进行的,该怎么办?
现在,当有人点击出价按钮,我发送请求到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';
}
}
你能给我推荐一下最好的方法吗?谢谢
发布于 2013-08-22 21:18:35
一个非常通用的建议是在插入出价时锁定表。它将消除您所描述的任何竞争条件的可能性。你读过这篇文章吗:
dev.mysql.com/doc/refman/5.5/en/innodb-locking-reads.html
https://stackoverflow.com/questions/18373055
复制相似问题