Refreshing a web page inserts data again to DB Refreshing a web page inserts data again to DB database database

Refreshing a web page inserts data again to DB


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


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).


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.