热搜:NVER node 开发 php

php 如何处理抽奖中的重复的兑换码

2024-09-14 12:55:02
php 如何处理抽奖中的重复的兑换码

做一个抽奖活动,设计表:
奖品表option:
oid   奖品ID
name  奖品名

兑换码表code:
oid  奖品ID
code  兑换码
status 是否使用 0:未使用 1:已使用

现在要求可以加入重复兑换码,而且兑换码通过文本框换行得到


编辑奖品和奖品兑换时如何处理code表的status呢,如果有相同的code,就不能直接update code set status=1 where code='{$code}'了。


回复讨论(解决方案)

如果 $code 是多行的兑换码
那么可以
$code = join(',', preg_split("/[\r\n]+/", $code));
处理成以逗号分隔的兑换码串
使用时
where code in ($code)
即可

如果 $code 是多行的兑换码
那么可以
$code = join(',', preg_split("/[\r\n]+/", $code));
处理成以逗号分隔的兑换码串
使用时
where code in ($code)
即可

有重复的怎么处理,比如现在有个人获奖了,兑换码是11,但是数据库中有两个11,怎么更新status

为什么要允许有重复的呢?
真有重复也可以
select oid from  code where code='yourcode' and status=0 limit 1
如果有结果oid,那么更新就是
update code set status=1 where oid='oid'