【问题标题】:php mysql pdo auction biddingphp mysql pdo 拍卖竞标
【发布时间】:2013-03-11 07:36:45
【问题描述】:

我正在使用 php mySQL(MyISAM) 和 PDO 开发一个具有限制或最高出价场景的拍卖系统,使用两个表

拍卖

 Id   | title | seller  |  currentbid   |  limit(max bid)   |  dated | bidder

出价

 Id  | auctionid | bid  | bidder | dated

用户出价后会发生以下情况(我正在使用 PDO 准备好的语句)

 $record = fetch (‘Select * from auction where id = :id’, array(‘id’ => $id))
 $increment = 1;
 $postedBid = $_REQUEST[‘postedBid’];
 $currentBid = $record[‘currentbid’];
 $limitBid = $record[‘limit’];

 If($postedBid < $currentBid + $increment){
    Return;(without processing bid!)
 }else{
    If($limitBid !> $postedBid){
       Insert into bids (auctionid, bid, bidder, dated(timestamp)) values ($auctioned, $postedBid, ……);

       Update auction set currentbid = $postedBid, limit = $postedBid ….

       }else{

       Insert into bids (auctionid, bid, bidder, dated(timestamp)) values ($auctioned, $postedBid, ……);

       Insert into bids (auctionid, bid, bidder, dated(timestamp)) values ($auctioned, $postedBid + $increment, ……);

       Update auction set currentbid = $postedBid + increment, limit = $limitBid ….
      }
  }

我想知道两件事

1- 如果这种方法可行,我该如何改进

2- 如何避免同时出价。我不知道如何使用 PDO 应用锁。感谢使用锁或任何其他方法来处理此问题的任何示例查询

谢谢

(抱歉使用混合类型的代码)

【问题讨论】:

    标签: php mysql pdo


    【解决方案1】:

    我认为您需要在 MySQL 中的存储过程中执行此操作,该过程仅返回成功/失败消息。

    您需要将代码更改为如下所示:

    If($postedBid < $currentBid + $increment){
       Return;(without processing bid!)
    } else {
       $stmt = $db->prepare("CALL update_bid(?,?, ...)");
       $stmt->execute(array($parameter1, $parameter2, ...));
    }
    

    同时检查代码以帮助防止不必要的数据库调用,存储过程将有助于确保没有并发问题。

    【讨论】:

    • hmmm 太好了,实际上我是 mysql 的新手,不知道它有程序设施,我一定会调查的。谢谢
    • 5.0.0 中存在对存储过程的“基本支持”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多