如何用PHP正确管理投标系统?
我用PHP和MySQL开发了一个投标系统。 它在大多数情况下都很有效,但我注意到当报价非常接近时会出现问题如何用PHP正确管理投标系统?,php,mysql,Php,Mysql,我用PHP和MySQL开发了一个投标系统。 它在大多数情况下都很有效,但我注意到当报价非常接近时会出现问题 $now = DateTime::createFromFormat('U.u', microtime(true)); $dateMicroTime = date("Y-m-d H:i:s").".".$now->format("u"); $amountToRaise = 100; $lastOffer = $bid->lastOffer(); //if there is an
$now = DateTime::createFromFormat('U.u', microtime(true));
$dateMicroTime = date("Y-m-d H:i:s").".".$now->format("u");
$amountToRaise = 100;
$lastOffer = $bid->lastOffer();
//if there is an offer yet
if($lastOffer){
$newPrice = $lastOffer->getAmount()+$amountToRaise;
//else is the first offer
}else{
$newPrice = $amountToRaise;
}
//if the user is not the last bidder
if($user->getId() != $lastOffer->getUserId()){
$bidOffer = new BidOffer();
$bidOffer->setBidId($bid->getId());
$bidOffer->setUserId($user->getId());
$bidOffer->setAmount($newPrice);
$bidOffer->setTime($dateMicroTime);
$bidOffer->save();
//if this is not the first offer I give back the money to the previous user
if($lastOffer){
$lastUser = $lastOffer->user();
$lastUser->setCash($lastUser->getCash()+$lastOffer->getAmount());
$lastUser->save();
}
}
当在不同时刻提供服务时,代码运行良好,但用户在相同的时间提供服务,例如:18:00:01.1299022和18:00.02.1222377
以前提供过优惠的用户没有收到优惠。
我怎样才能解决这个问题?我尝试使用一个临时变量来阻止临时语句,直到每个查询都被执行,但没有成功。我将dateTime与microtime分开,并且不会使用
$dateMicroTime=date(“Y-m-d H:I:s”)。“$now->format(“u”)代码>
您可以使用微时间提取最后一个投标人。这可以通过在数据库中添加bid_utime列来实现。如果您按时间顺序查找一次拍卖的所有投标人,请按表排序。bid\u utime DESC
。最后一位投标人可按表排序找到。bid\u utime DESC LIMIT 1作为$lastOffer=$bid->lastOffer()的返回代码>
这也意味着您将不会通过以下方式保存您的出价:$bidfore->setTime($dateMicroTime)代码>但使用类似以下内容:
$bidfore->setDate($date)代码>和$bidfore->set_uTime($now)代码>
但您也可以跳过所有这一切,只返回投标表中的最后一个条目,并使用SELECT*from bid_table ORDER BY ID DESC LIMIT 1
,忘记dateMicroTime和microtime。希望这能有所帮助。我会将dateTime和microtime分开,并且不会使用$dateMicroTime=date(“Y-m-dh:I:s”)。“$now->format(“u”)代码>
您可以使用微时间提取最后一个投标人。这可以通过在数据库中添加bid_utime列来实现。如果您按时间顺序查找一次拍卖的所有投标人,请按表排序。bid\u utime DESC
。最后一位投标人可按表排序找到。bid\u utime DESC LIMIT 1
作为$lastOffer=$bid->lastOffer()的返回代码>
这也意味着您将不会通过以下方式保存您的出价:$bidfore->setTime($dateMicroTime)代码>但使用类似以下内容:
$bidfore->setDate($date)代码>和$bidfore->set_uTime($now)代码>
但您也可以跳过所有这一切,只返回投标表中的最后一个条目,并使用SELECT*from bid_table ORDER BY ID DESC LIMIT 1
,忘记dateMicroTime和microtime。希望这有帮助