停止两次将数据插入数据库 [英] Stop data inserting into a database twice

查看:96
本文介绍了停止两次将数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP还是很陌生,我想知道当用户在与表单相同的页面上刷新时,其他程序员使用什么方法/预防措施阻止将数据两次输入到MySQL数据库中?显然它发生了,我需要一个很好的方法来阻止这种情况.

Im quite new to PHP, i was wondering what methods/preventions other programmers use to stop data being entered twice into a MySQL database when a user refreshes on the same page as a form? Obviouly it happens and i need a good way to stop this.

谢谢,本

推荐答案

我将此称为网络编程的黄金法则:

I call this a golden rule of web programming:

永远不会用主体来响应POST请求.始终执行工作,然后使用Location:标头响应以重定向到更新的页面,以便浏览器使用GET请求它.

Never ever respond with a body to a POST-request. Always do the work, and then respond with a Location: header to redirect to the updated page so that browser requests it with GET.

这样,刷新不会对您造成任何伤害.

This way, refreshing will not do you any harm.

此外,关于此处评论中的讨论.为了防止重复发布(例如,意外地双击提交"按钮),将表单的md5()存储在文本文件中,并将新表单的md5与存储的md5进行比较.如果它们相等,那么您将获得双重职位.

Also, regarding a discussion here in comments. To protect from double posting from, say, accidental doubleclicking the Submit button, store an md5() of your form in a text file, and compare the new form’s md5 to the stored one. If they are equal, you are having a double post.

这篇关于停止两次将数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆