PHP /MYsql 中的重复问题
我正在 mysql 表中插入一行。该行已成功插入表中。 但问题是,每当我刷新页面时,数据库中就会添加一个新行。
如何预防呢?
I am inserting a row in mysql table. The row is getting inserted successfully in the table.
But the issues is that whenever I refresh the page, a new row is added in the database.
How to prevent it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用Post/Redirect/Get 模式。
在 PHP 中,通常看起来像这样......
显然
handleForm()
将替换处理插入等的代码。Use the Post/Redirect/Get pattern.
In PHP, this looks like so, generally...
Obviously
handleForm()
is to be substituted for your code that handles the inserts etc.这是浏览器的问题。他们在刷新时重新发布数据。为了防止这种情况发生,每次插入新记录时,您都必须检查代码中的数据是否重复。这是必须防止数据重复的一般做法。
在另一种情况下,如果期望并允许真正的重复数据,您可以做的是在每次插入后将用户重定向到相同的注册页面
header("Location: {$location}");
。这将清除浏览器的 POST 数据。This is browser's issue. They repost data on refresh. To prevent it from happening, you'll have to check for duplication of data in your code each time a new record is inserted. This is the general practice where duplication of data has to be prevented.
In another case where a genuine duplicate data is expected and allowed, what you can do is to redirect user on to the same registration page after each insertion by
header("Location: {$location}");
. This will clear the POST data of the browser.在mysql表中使用主键。将任意列设为主键列
Use primary key in mysql table. Make any column as primary key column