2013-03-11 88 views
0

我正在开发一个拍卖系统的限制或使用PHP的MySQL(MyISAM数据)和PDO最高出价的情况下,用两个表PHP MySQL的PDO招标拍卖

拍卖

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

投标

Id | auctionid | bid | bidder | dated 

用户下了投标后,发生以下情况(我正在使用PDO准备的语句S)

$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来应用锁。欣赏使用锁或任何其他的办法来处理这个问题

感谢

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

回答

0

我认为你有必要做这个存储过程在什么任何例子查询简单地返回成功/失败消息的MySQL。

你需要改变你的代码是这样的:

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

让你检查的代码以及帮助避免不必要的数据库调用,存储过程只会有助于确保没有并发问题。

+0

嗯,其实我是一个新手到MySQL不知道它有程序设施,我一定会考虑它。感谢 – Shah 2013-03-11 08:02:49

+0

存储过程的“基本支持”存在于5.0.0中 – Talon 2013-03-11 08:27:11