【问题标题】:Need suggestion for insertion of two records in mysql at same time需要在mysql中同时插入两条记录的建议
【发布时间】:2013-08-22 06:22:38
【问题描述】:

我在拍卖网站上工作,用户可以在该网站上出价并赢得拍卖。在我的情况下,竞标结束时拍卖结束(从管理员设置的出价数量)。所以我需要关于最终出价的建议。如果最终出价是由 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';
    }

 }

你能建议我最好的方法吗?谢谢

【问题讨论】:

标签: php jquery html mysql ajax


【解决方案1】:

一个非常通用的建议是在插入出价时锁定表格。它将消除您描述的竞争条件的任何可能性。你读过这个吗:

dev.mysql.com/doc/refman/5.5/en/innodb-locking-reads.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 2023-02-04
    • 2017-01-31
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多