Take the 2-minute tour ×

I have a function that imports a files contents into mySQL and returns the results. If i refresh the page and click "yes" it will do it again doubling the output with the same content.

How can I stop this happening? In this particular case there is no URI in the address bar but on other functions there is.

share|improve this question
Have your script check whether that record is already in the database? –  Oli Charlesworth Apr 17 '11 at 15:53
Could do, but the only way to check is on the filename, and if the filename already exists it renames it and adds it anyway. –  Ezra Apr 17 '11 at 15:54
add comment

4 Answers

up vote 2 down vote accepted

You should just check if the contents in the database exist, if they don't, fill them. Otherwise don't run the function.


if !database.containsRecords

On top of this, it is always good practice to redirect after a POST request. So you would want:

header("Location page.php");
share|improve this answer
Can i pass a success msg along with that? –  Ezra Apr 17 '11 at 16:17
@Ezra: Have the destination page say "success". –  Oli Charlesworth Apr 17 '11 at 16:19
Not quite the most dynamic of processes. –  Ezra Apr 17 '11 at 16:21
@Ezera, yes. Just store the message in a session variable. Look up php.net/manual/en/ref.session.php –  Mike Lewis Apr 17 '11 at 16:23
Can you elaborate more? –  Mike Lewis Apr 17 '11 at 16:28
show 3 more comments

Query the database on each page load and see if it has already been populated. If it has been populated then don't attempt to populate it again.

share|improve this answer
add comment

You should use the POST-redirect-GET pattern.

After updating the database, send an HTTP redirect to a separate page that displays the results.
Refreshing the browser will reload that separate page.

share|improve this answer
And using the browser back button will mess up anyway. Yes some modern browsers prevent resending POST data but not all. –  Oliver A. Apr 17 '11 at 15:58
Yup you really should do a redirect on success. Actually you should do this for all forms. It is best practise. –  PeeHaa Apr 17 '11 at 16:00
@Oliver: nopez. the back button shows the empty form again. –  PeeHaa Apr 17 '11 at 16:01
@Oliver: Wrong. Redirects do not create separate entries in history; going back will go back to the page that submitted the form. –  SLaks Apr 17 '11 at 16:03
Yes if you do it right. Some redirects DO create entities and sometimes bad redirects even trap you on a page. –  Oliver A. Apr 17 '11 at 16:11
show 1 more comment

I assume you use a POST form to upload the file. You can include a hidden input field with an pseudo random unique id in your form. If the user resends the data via POST you can check if you already processed this request. Hidden fields are not save because a user might edit them, but you can detect accidental resubmits.

share|improve this answer
add comment

Your Answer


