我正在拍卖网站上工作,用户可以出价并赢得拍卖。在我的情况下,拍卖结束时出价(从管理员设置的出价数量)。所以我需要有关最终出价的建议。如果最终出价由两个不同的用户同时完成,该怎么办?
现在当有人点击出价按钮时,我正在向ajax发送请求并在那里插入记录。我知道这样做的唯一方法是从数据库中检查并留下多少出价并相应地插入(通过允许一个用户),但如果时间完全相同,则可能再次发生异常。同时我不想浪费时间在那里再次查询数据库,因为它会延迟竞标并可能造成更多麻烦。
这是我的出价代码。
$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';
}
}
$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';
}
}
你能告诉我最好的方法吗?感谢
答案 0 :(得分:1)
一个非常通用的建议是在插入出价时锁定表格。它将消除你描述的竞争条件的任何可能性。你读过这个: