Data doubling in database on F5

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
itp
Forum Commoner
Posts: 67
Joined: Fri Jun 15, 2007 6:50 am

Data doubling in database on F5

Post by itp »

I have seen this problem in other sites and have come across occationally in my work. Say you have a web page where user enters information and clicks button to submit. If user presses then presses F5 or backs up and clicks submit again information will be doubled in database, if no validation is done to detect duplicate records, or duplicate keys. However, sometimes there is no unique key entered by the user or key is just a sequential number (like a comment on a blog or an oline order for a magazine). In this case it is hard to determine what is an invalid duplicate entry.

One way I have been avoiding upwanted duplicates if by generating a random number when users enters HTML form for the first time or after database update. I pass this number from page to page on as a hidden value on the form submit button. I also put this value in a $_SESSION variable. When it is time to update the database, I check that $_SESSION value = get/post value. If equal, program will insert data to database, show confirmation screen and generate new random number. If not equal, program will give error message, generate a new random number and show new blank form.

Code: Select all

$_SESSION['guestAddedToken'] = $this->fnGenerateToken();
I wonder how other people handle this issue.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Data doubling in database on F5

Post by Benjamin »

Just send a header redirect to the landing page.
itp
Forum Commoner
Posts: 67
Joined: Fri Jun 15, 2007 6:50 am

Re: Data doubling in database on F5

Post by itp »

Gee why didn't I think of that? I guess that would work for POSTs, but not for GETs.
Also this would not prevent someone from hitting back button & submit a second time.
Post Reply