Page 1 of 1

clean up POST values after submitting form

Posted: Wed Oct 04, 2006 8:15 am
by ansa
Hi,

I have a form that inserting data into my database. After successfully submitting it and if I hit F5 or refresh the page, the same data will be submitted again, so it can ruin my database.

How to avoid this?

Posted: Wed Oct 04, 2006 8:41 am
by twigletmac
You can do a few things, for example:
  • redirect the user to another page once the information they posted has been processed (see the header() function); or
  • compare the posted data with the data in the database to see if there are any exact matches
Mac

Posted: Fri Oct 06, 2006 6:02 am
by ansa
I've tried with header, but an error message came up:
warning: Cannot add header information - headers already sent in...bla bla bla...

as a I substitute, I use meta refresh, set the content to 0. Is it safe to use this method?

Posted: Fri Oct 06, 2006 6:47 am
by aaronhall
You must invoke header() before any output has been sent to the browser (even a single space will throw that same error). But you should always check that the same information has not been added to the database before you add new information. If you use header(), hitting the back button in your browser may rerun the script and add a duplicate entry. Here's something simple to check if the information has already been added to the database (adjust the SQL query for your table):

Code: Select all

<?
if(mysql_result(mysql_query("SELECT count(*) FROM table WHERE field1 = '$sentData1' AND field2 = '$sendData2'"),0)) {
   // don't insert the data
} else {
   // it's okay to insert the data
}
?>