Ensuring a web page refresh doesn"t create a new record
Ensuring a web page refresh doesn"t create a new record
am 18.11.2005 16:38:02 von Sam
Hi,
Another question:
As per my previous post, my database is very simple: User ID, Country,
Postcode and Date.
This information is entered through the browser via a standard form
which works well.
Problem: If the user clicks on the browser's refresh button, the same
data gets added to my SQL database as a new record. Everytime refresh
is hit, a new record is added with the same data.
What's the best way to prevent this from happening?
I know you can set fields as being unique in MySQL, but can you set -
say - an entire row? So another duplicate row containing all of the
same values could not be written? This would be sufficient for my
needs!
Thanks in advance!
Sam Day.
Re: Ensuring a web page refresh doesn"t create a new record
am 18.11.2005 18:58:57 von Stefan Rybacki
Sam wrote:
> Hi,
>
> Another question:
>
> As per my previous post, my database is very simple: User ID, Country,
> Postcode and Date.
>
> This information is entered through the browser via a standard form
> which works well.
>
> Problem: If the user clicks on the browser's refresh button, the same
> data gets added to my SQL database as a new record. Everytime refresh
> is hit, a new record is added with the same data.
>
> What's the best way to prevent this from happening?
>
> I know you can set fields as being unique in MySQL, but can you set -
> say - an entire row? So another duplicate row containing all of the
> same values could not be written? This would be sufficient for my
> needs!
This is what Unique indices do. You could also use replace statements if you have unique
or primary key values. Replace is similar to insert except that it updates a row if
another row with the given primary key or unique value already exists.
Another way is to store whether you already saved the record. Or use an additional script
like this:
This way refreshing would only refresh the result page and not the insert script.
Regards
Stefan
>
> Thanks in advance!
>
> Sam Day.
>
Re: Ensuring a web page refresh doesn"t create a new record
am 18.11.2005 23:04:14 von jds
On Fri, 18 Nov 2005 07:38:02 -0800, Sam wrote:
> Problem: If the user clicks on the browser's refresh button, the same
> data gets added to my SQL database as a new record. Everytime refresh
> is hit, a new record is added with the same data.
To prevent this sort of thing, I always separate out my "action"
components from my "display" components, and use header("Location: ...");
to redirect to the appropriate destination page, usually with a success or
error message.
That is to say, I never have the PHP component that displays stuff to the
browser be the same component that processes input and updates any
relevant database.
In any case, to make this work, use *two* (or possibly three) pages -- one
that has the HTML form for user input and and one that processes the data
but never actually prints anything to the browser.
page1.php (some important details ommitted from this example):
Re: Ensuring a web page refresh doesn"t create a new record
am 19.11.2005 01:37:05 von salmondays2000
>> In any case, to make this work, use *two* (or possibly three) pages -- one
>> that has the HTML form for user input and and one that processes the data
>> but never actually prints anythingto the browser.
OK - thanks Stefan and Jeff - very useful info.
I'll display a "Thank you" type page once the submit has taken place.
This seems like a good workaround for me.