【发布时间】:2017-01-17 08:50:15
【问题描述】:
我在 MySQL 中使用 CodeIgniter。案例是,客户购物,并用帐户付款。
代码如下:
$globallockname = 'MEM_LOCK_ORDER_PAYLOCK';
//lock with memcache add
if (!$this->getGlobalLock($globallockname, 10)){
dexit(array('error'=>true,'msg'=>'get loack failed'));
}
//check if account is enough
$member = $this->admin_model->getItem('member', array('id'=>$this->_member['id']), false);
if ($this->_member['account'] >= $amountpay){]
//pay
$this->admin_model->update('member', "`account` = `account` - ".$amountpay, "`id` = ".$this->_member['id']);
}else{
//unlock
$this->delGlobalLock($globallockname);
dexit(array('error'=>true,'msg'=>'account is short, please recharge'));
}
//unlock
$this->delGlobalLock($globallockname);
当客户点击“购买”一次,一切正常,但如果他们在短时间内点击“购买”两次,此脚本将执行两次,第一次更新将不起作用。
我检查了$db->query的返回值,每个都返回true。
【问题讨论】:
-
“短时间”有多短?不到一秒?几分钟?
-
@Simba,在 1 或 2 秒内。
-
@Shoyeb Sheikh,用完整代码更新问题。
标签: php mysql codeigniter