clean up POST values after submitting form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ansa
Forum Commoner
Posts: 31
Joined: Tue Aug 29, 2006 1:58 am
Location: Hamburg, Germany

clean up POST values after submitting form

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
ansa
Forum Commoner
Posts: 31
Joined: Tue Aug 29, 2006 1:58 am
Location: Hamburg, Germany

Post 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?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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
}
?>
Post Reply