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?
clean up POST values after submitting form
Moderator: General Moderators
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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
- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
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
}
?>