刷新网页会再次将数据插入数据库

时间:2021-05-08 16:25:41

I've a php web page with a form. After filling the form in web page, I can submit it to the server. But after that, if I refresh the page, it inserts the same record to the database. Is there any solutions to this problem?

我有一个带有表单的php网页。在网页中填写表单后,我可以将其提交给服务器。但在那之后,如果我刷新页面,它会将相同的记录插入数据库。这个问题有什么解决方案吗?

7 个解决方案

#1


9  

Use the POST/Redirect/GET pattern. This will prevent the user from being able to resubmit the same form.

使用POST / Redirect / GET模式。这将阻止用户重新提交相同的表单。

#2


4  

There are a number of ways, for example:

有很多方法,例如:

  • Create a token which you insert into the form (hidden field). if the same token is submitted twice, discard the second submit.
  • 创建一个插入表单的标记(隐藏字段)。如果同一令牌被提交两次,则丢弃第二次提交。
  • Check in the database if an identical post already exists.
  • 如果已存在相同的帖子,请检入数据库。
  • Save the form submit in a session variable.
  • 将表单提交保存在会话变量中。
  • Redirect the user to a second page after the submit, using the Post/Redirect/Get pattern (preferably in combination with one of the above).
  • 使用Post / Redirect / Get模式(最好结合上述之一),在提交后将用户重定向到第二页。

#3


1  

Yep, do two queries. The first checks to make sure the data doesn't already exist in the DB, the second one does the insert if there is no duplicate data. There are a bunch of ways to go about checking to make sure the duplicate data doesn't exist, but this is the basic process you will want to go through.

是的,做两个查询。第一个检查以确保数据不存在于DB中,第二个检查是否存在重复数据。有很多方法可以检查以确保不存在重复数据,但这是您希望通过的基本过程。

#4


1  

Well, that's what Refresh means: "do it again."

嗯,那就是刷新意味着:“再做一次。”

You can check to see if data that matches the submitted data is already in the database. If so, you can reject the new submission.

您可以检查与提交的数据匹配的数据是否已存在于数据库中。如果是这样,您可以拒绝新提交。

#5


0  

Before inserting to the database check whether the same values are duplicates already, and if they are duplicates, don't insert. Checking for multiple columns helps even further. For example, instead of checking just for a "username", you would check for a "username" AND "password".

在插入数据库之前,检查相同的值是否已经重复,如果它们是重复的,请不要插入。检查多列有助于进一步发展。例如,您不会只检查“用户名”,而是检查“用户名”和“密码”。

Obviously the examples above are fake, but you should get the point.

显然上面的例子是假的,但你应该明白这一点。

#6


0  

You can make a process.php page and set your form's action to it.

您可以创建一个process.php页面并将表单的操作设置为它。

In process.php

在process.php中

//code to insert item to database
header('Location: YOUR_FORM_PAGE_HERE);

Then it will send them back to the original page and there won't be any post data

然后它会将它们发送回原始页面,并且不会有任何发布数据

#7


-1  

You could change your query to INSERT IGNORE INTO table_name ...

您可以将查询更改为INSERT IGNORE INTO table_name ...

this will prevent you from inserting twice.

这将阻止您插入两次。

#1


9  

Use the POST/Redirect/GET pattern. This will prevent the user from being able to resubmit the same form.

使用POST / Redirect / GET模式。这将阻止用户重新提交相同的表单。

#2


4  

There are a number of ways, for example:

有很多方法,例如:

  • Create a token which you insert into the form (hidden field). if the same token is submitted twice, discard the second submit.
  • 创建一个插入表单的标记(隐藏字段)。如果同一令牌被提交两次,则丢弃第二次提交。
  • Check in the database if an identical post already exists.
  • 如果已存在相同的帖子,请检入数据库。
  • Save the form submit in a session variable.
  • 将表单提交保存在会话变量中。
  • Redirect the user to a second page after the submit, using the Post/Redirect/Get pattern (preferably in combination with one of the above).
  • 使用Post / Redirect / Get模式(最好结合上述之一),在提交后将用户重定向到第二页。

#3


1  

Yep, do two queries. The first checks to make sure the data doesn't already exist in the DB, the second one does the insert if there is no duplicate data. There are a bunch of ways to go about checking to make sure the duplicate data doesn't exist, but this is the basic process you will want to go through.

是的,做两个查询。第一个检查以确保数据不存在于DB中,第二个检查是否存在重复数据。有很多方法可以检查以确保不存在重复数据,但这是您希望通过的基本过程。

#4


1  

Well, that's what Refresh means: "do it again."

嗯,那就是刷新意味着:“再做一次。”

You can check to see if data that matches the submitted data is already in the database. If so, you can reject the new submission.

您可以检查与提交的数据匹配的数据是否已存在于数据库中。如果是这样,您可以拒绝新提交。

#5


0  

Before inserting to the database check whether the same values are duplicates already, and if they are duplicates, don't insert. Checking for multiple columns helps even further. For example, instead of checking just for a "username", you would check for a "username" AND "password".

在插入数据库之前,检查相同的值是否已经重复,如果它们是重复的,请不要插入。检查多列有助于进一步发展。例如,您不会只检查“用户名”,而是检查“用户名”和“密码”。

Obviously the examples above are fake, but you should get the point.

显然上面的例子是假的,但你应该明白这一点。

#6


0  

You can make a process.php page and set your form's action to it.

您可以创建一个process.php页面并将表单的操作设置为它。

In process.php

在process.php中

//code to insert item to database
header('Location: YOUR_FORM_PAGE_HERE);

Then it will send them back to the original page and there won't be any post data

然后它会将它们发送回原始页面,并且不会有任何发布数据

#7


-1  

You could change your query to INSERT IGNORE INTO table_name ...

您可以将查询更改为INSERT IGNORE INTO table_name ...

this will prevent you from inserting twice.

这将阻止您插入两次。