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

标签 php mysql codeigniter

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

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;

仅当特定拍卖 ID 的最高出价低于 500 时,才会插入新的拍卖;

关于php - 使用php同时处理数据库中的多个条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46558423/

相关文章:

AWS Linux 服务器上的 PHPMailer 超时

mysql - 当 MySQL 告诉我 "Access Denied"时,为什么它认为我的用户名是 '' ?

mysql - 为mysql中的每个派生表设置别名

php - 使用 PHP 在 Codeigniter 中连接两个表

php - Codeigniter user_agent 不工作

php - Symfony Twig 错误 : Unknown "^macro_name^" function. 您的意思是 "^similar_function_name^"吗?

php - 简单的注册表单不会插入数据库

php - 创建后 Laravel 5 Eloquent 加载模型属性

mysql - 对两个没有连接的表进行组合 SELECT 查询

php - PHP 和 MySQL 的 Multi-Tenancy