By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Preventing Duplicate Record Insertion on Page Refresh

P: n/a
I am developing a PHP-mysql database. It is noted that when the
browser window is refreshed the data is inserted again in the
database. unfortunately there is no unique keys that I can use to
verify the existance of the data, so as to prevent the multiple
insertion.
Dec 11 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
rf

"chowdary" <ph*********@gmail.comwrote in message
news:35**********************************@d4g2000p rg.googlegroups.com...
>I am developing a PHP-mysql database. It is noted that when the
browser window is refreshed the data is inserted again in the
database. unfortunately there is no unique keys that I can use to
verify the existance of the data, so as to prevent the multiple
insertion.
This question is asked at least once per week in many newsgroups.

As you are using the dreaded Google groups to view this newsgroup why don't
you use Google to search the archive for solutions?
Dec 11 '07 #2

P: n/a
On 11 Dec, 11:07, chowdary <php.quer...@gmail.comwrote:
I am developing a PHP-mysql database. It is noted that when the
browser window is refreshed the data is inserted again in the
database. unfortunately there is no unique keys that I can use to
verify the existance of the data, so as to prevent the multiple
insertion.
http://www.boutell.com/newfaq/creating/stoprefresh.html
Dec 11 '07 #3

P: n/a
chowdary wrote:
I am developing a PHP-mysql database. It is noted that when the
browser window is refreshed the data is inserted again in the
database. unfortunately there is no unique keys that I can use to
verify the existance of the data, so as to prevent the multiple
insertion.
So you have a problem.
And since you cannot avoid a refresh of the screen...

If you are doing a form post, you might consider sending a location
header AFTER you inserted the record:

<form action="process.php" method="POST">
<input type="text" name="number">
<input type="sumbit">
</form>

then from process.php:
// do you usual inserts in the database based on the post

header("Location: http://www.example.com/thanks.php");
// do not forget the exit, since your script will run on without it.
exit;
In that way your script will process the posting, and then redirects the
browser to thanks.php.
A reload of thanks.php will not result in a fresh db insert.

Hop ethat helps.

Regards,
Erwin Moller
Dec 11 '07 #4

P: n/a
On Dec 11, 4:11 pm, Erwin Moller
<Since_humans_read_this_I_am_spammed_too_m...@spam yourself.comwrote:
chowdary wrote:
I am developing a PHP-mysql database. It is noted that when the
browser window is refreshed the data is inserted again in the
database. unfortunately there is no unique keys that I can use to
verify the existance of the data, so as to prevent the multiple
insertion.

So you have a problem.
And since you cannot avoid a refresh of the screen...

If you are doing a form post, you might consider sending a location
header AFTER you inserted the record:

<form action="process.php" method="POST">
<input type="text" name="number">
<input type="sumbit">
</form>

then from process.php:
// do you usual inserts in the database based on the post

header("Location:http://www.example.com/thanks.php");
// do not forget the exit, since your script will run on without it.
exit;

In that way your script will process the posting, and then redirects the
browser to thanks.php.
A reload of thanks.php will not result in a fresh db insert.

Hop ethat helps.

Regards,
Erwin Moller
Thankyou Erwin Moller, very much, i got the correct result...great help
Dec 11 '07 #5

P: n/a
chowdary wrote:
unfortunately there is no unique keys that I can use to verify the
existance of the data, so as to prevent the multiple insertion.
Why not add "IP address" and "timestamp" columns to the table, and use
that combination as a key?

It's often quite handy to store that information anyway.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 3 days, 23:09.]

Sharing Music with Apple iTunes
http://tobyinkster.co.uk/blog/2007/1...tunes-sharing/
Dec 11 '07 #6

P: n/a
On Dec 11, 7:34 am, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
chowdary wrote:
unfortunately there is no unique keys that I can use to verify the
existance of the data, so as to prevent the multiple insertion.

Why not add "IP address" and "timestamp" columns to the table, and use
that combination as a key?

It's often quite handy to store that information anyway.
Because it's quite possible that two posts from the same IP address
occur at exactly the same moment -- especially with NAT.
Dec 11 '07 #7

P: n/a
Greetings, chowdary.
In reply to Your message dated Tuesday, December 11, 2007, 14:07:48,
I am developing a PHP-mysql database. It is noted that when the
browser window is refreshed the data is inserted again in the
database. unfortunately there is no unique keys that I can use to
verify the existance of the data, so as to prevent the multiple
insertion.
Another possible solutions.

If You're using sessions, generate serial and supply it as hidden field with
every posting form. Increase it at every post and decline to store data if
serial in the _POST isn't equal to the one stored in _SESSION.

Combining it with redirects (always good to have such protection), You'll
probably never got the duplicated submits.
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Dec 12 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.