类似新手卡发放如何防止重复

WBOY
Release: 2016-06-13 11:49:55
Original
1167 people have browsed it

类似新手卡发放怎么防止重复
我现在有一个需求,一个数据表中已经预先插入了许多新手卡卡密,然后需要本站每个会员自行去领取。但是问题来了,如果在一个很短的时间段有很多人都去领取这个新手卡,怎么设计,才能不重复
------解决方案--------------------
这个问题是关于并发的问题,解决办法是加锁,sql加一个行级锁就行了。以前我也做过类似的,这个必须加锁,可以在数据库里加锁,可以在代码中对那函数加锁, 你可以去查一下类似数据库加锁的资料。
------解决方案--------------------
方法很多,可以参考一下乐观锁悲观锁那些
------解决方案--------------------
我新手,刚刚自己想的,可能在逻辑上会有漏洞..你还是查他们说的名词比较靠谱~
------解决方案--------------------
用一个字段记录卡是否已被使用,然后利用mysql 的update 锁表来解决并发问题。

例如表结构为
id cardno cardpass used

$sqlstr = "update table set used=1 where used=0 and cardno=xxxx";
mysql_query($sqlstr);
$affected_rows = mysql_affected_rows();
if($affected_rows==1){ // 表示修改成功
   // 执行使用成功处理
}else{
   // 执行卡已被使用处理
}

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!