Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refreshing a web page inserts data again to DB

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?

like image 838
Joe Avatar asked Jun 28 '10 16:06

Joe


3 Answers

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

like image 137
D'Arcy Rittich Avatar answered Oct 07 '22 18:10

D'Arcy Rittich


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).
like image 28
Emil Vikström Avatar answered Oct 07 '22 19:10

Emil Vikström


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.

like image 28
Mike Keller Avatar answered Oct 07 '22 17:10

Mike Keller