For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > November 2005 > Ensuring a web page refresh doesn't create a new record









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Ensuring a web page refresh doesn't create a new record
Sam

2005-11-18, 6:59 pm

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.

Stefan Rybacki

2005-11-18, 6:59 pm

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:

Input -- script call --> insert.php -- redirect --> result page

This way refreshing would only refresh the result page and not the insert script.

Regards
Stefan

>
> Thanks in advance!
>
> Sam Day.
>

JDS

2005-11-18, 6:59 pm

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.

<aside>On the web and in books, you see a lot of examples of PHP scripts
using $PHP_SELF as the action="" in an HTML form. This, IMO, is horrible,
horrible programming practice on several levels.</aside>

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):
<form action="process_form.php">
<input name="butt">
<input type="submit">
</form>


process_form.php:
<?php
$sql = "INSERT INTO chs VALUES (1, '" . $_GET['butt'] . "')";
mysql_query($sql);
header("Location: page2.php?message=success");
?>

page2.php:
<?php
print $_GET['message'];
?>


later...
--
JDS | jeffrey@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Sam

2005-11-18, 6:59 pm

>> In any case, to make this work, use *two* (or possibly three) pages -- one[color=darkred]

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.

Best regards,

Sam Day.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com