Home  >  Article  >  Backend Development  >  问关于sql语句防止用户重复提交有时候不起作用解决方案

问关于sql语句防止用户重复提交有时候不起作用解决方案

WBOY
WBOYOriginal
2016-06-13 13:37:02823browse

问关于sql语句防止用户重复提交有时候不起作用
有一个问答表单,用户选择好选项之后提交,我先用一个sql语句加上限制,使得回答正确的人就不用再回答了:

SQL code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->select * from q_record where username='$username' and  isright=1 limit 1


如果找不到,则执行插入语句:
SQL code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->insert into q_record...


但是发现,数据库里面仍然会有个用户的isright=1的数量大于1,而且时间是同一秒的,不知道是什么原因?是不是数据库反应不过来了,还没有做好select判断,就执行了insert语句?

------解决方案--------------------
我想看看你的php代码. 另外SQL也不需要这么写.
------解决方案--------------------
你这个不是同个表么?q_record 怎么是用insert into 不是update set?


------解决方案--------------------
加个时间限制,js和php都限制,js限制是一般情况的限制,php再在session中存放上次回答时间,到时候进行限制就行了,如果问题是因为极短时间内重复提交的原因
------解决方案--------------------
探讨

引用:
你这个不是同个表么?q_record 怎么是用insert into 不是update set?
insert into是记录用户的回答记录,如果已经答对了,就提示用户不用再答了,否则就让用户再来回答。

------解决方案--------------------
说起这个防止用户重复提交,连号称最大社区的csdn也没解决,,昨天一个getTime()的问题,,我只提交了一遍,出来显示2个,扣了40分,我擦
------解决方案--------------------
$query = "select * from q_record where username='$username' and isright=1 limit 1";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if ($row) {
echo "你已经答对该题了,不需要再回答哦!";
 
}else{
$query = "insert into q_record (pid,question,isright,time) values ('$pid','$question','$isright','$time')";
$result = mysql_query($query);
}
?>
这样试一下
------解决方案--------------------
数据库表的结构是什么样的。
Statement:
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 admin@php.cn