繁体   English   中英

使用php同时处理数据库中的多个条目

[英]handle multiple entries at same time in database using php

我有一个拍卖表,其中有多个用户竞价拍卖

ID |  BidderID |  AuctionID |  HighestBidPrice
------------------------------------------------
1  |  1        |  35        |  100
2  |  2        |  35        |  200
3  |  3        |  35        |  200
4  |  2        |  35        |  300
5  |  3        |  35        |  300

当2个用户同时出价时,两个出价都将插入到表格中。 我正在尝试编写代码来处理特定的AuctionID的这种类型的死锁条件。 用户甚至不应插入比最后HighestBidPrice少的价格。

我通过查询查询表中的条件

$last_bid = $this->db->query("select * from Auction as A where A.AuctionID = 35 order by ID desc limit 1")->result_array();

首先,我得到最后的最高出价,然后检查

if($last_bid){
        if(isset($last_bid[0]['HighestBidPrice']) && $last_bid[0]['HighestBidPrice']!=""){
            if($last_bid[0]['HighestBidPrice'] === $_POST['HighestBidPrice']){
                echo "The Bid with this price already made.";
            }
            if($last_bid[0]['HighestBidPrice'] >= $_POST['HighestBidPrice']){
                echo "The Bid price is less then Highest Bid Price.";
            }
        }
    }

即使多个用户同时插入一个出价,两个人也都可以进行出价。

交易能够解决这个问题吗?

您可以通过以下方式做到这一点:

insert into auction (BidderID, AuctionID, HighestBidPrice)
select 10, 20, 500
where (SELECT max(HighestBidPrice) FROM auction WHERE AuctionID = 20) < 500;

仅当该特定AuctionID的最高出价率小于500时,才会插入新的拍卖。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM