Forum Moderators: coopster

Message Too Old, No Replies

Click refresh re-executes db insert

         

chuender

5:27 am on Aug 9, 2005 (gmt 0)

10+ Year Member



I have a page which allows users to add a new contact to the database. On complete of this add, the user is taken to the contact list page, which lists all the contacts. That part works.

However, the problem occurs when user then clicks Refresh, on the contact list page, and the page refreshes and a duplicate contact is created.

How would I resolve this.

Thanks for your help.

madmac

5:40 am on Aug 9, 2005 (gmt 0)

10+ Year Member



before inserting the data to the database, check that a duplicate entry does not already exist for that user?

jd01

7:24 am on Aug 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I set a unique variable passed in the submission, usually $add with a value of Y. On a submission I check to see if($add==="Y") and if it does, connect to the db.

Then immediately after the DB connection I unset($add); This way, unless the user actually submits the form again, there can be no duplication, because there is no $add...

You can also unset($all,$your,$other,$variables); so there is no chance of a duplicate, unless the person fills out the form again.

Justin

dcrombie

11:44 am on Aug 9, 2005 (gmt 0)



The problem is that the INSERT is happening on the 'contact list' page and not on the 'add contact' page. The reload is re-issuing the POST variables. Ideally, the INSERT would happen on the 'add contact' page and then redirect to the contact list page.

;)

jd01

4:55 pm on Aug 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good point dcrombie,

Appearantly, early this AM while I was typing I forgot to cover that...

I usually put my connections at the top of the page, after the connection and the unset() if you choose to use it, you can easily reload the page (or any other) using:

header("Location: http://fullurl.com/your/troubled/page.php");

which will reload the page as a new request, and you should be fine... even if your connection is on the results page, because the variables will be unset and the page request will be sent as new to the server - like highlighting the URL in the browser bar and clicking return - reloads the page, but does not refresh the page.

Hope these help a little, like I said late last night and haven't had coffee yet today - ugly combination. They should at least give you an idea.

Justin

chuender

5:31 pm on Aug 9, 2005 (gmt 0)

10+ Year Member



Thanks for all the input. Actually, the insert does not happen in the List View page.

These pages are coded using the Fusebox methodology. What happens is from the Add Contact page, on click of Add button, the page does validation and inserts into the database. If teh insert went ok, then it loads the List View. The script for List View is very simple, which is just a plain read of the database, with no updates or inserts.

For some reason, when users click Refresh after they have added a new contact record and then brought back to List View, all the scripts which were executed after on click of Add button is re-executed.