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

Duplicate records added from post forms, easy solutions?

P: n/a
Hi,

When a user submits / posts data my php script Inserts data into my
database.
If they refresh the script or click back and click submit again I get
duplicate record.

Is there an easy solution to this?

Or do I need to create a hidden field which contains some sort of
unquie data, like a time stamp and have a have a unique field which
stops it adding the record?

Best regards,

Jules.

Jul 15 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
*** mindwarp escribió/wrote (15 Jul 2006 02:31:04 -0700):
If they refresh the script or click back and click submit again I get
duplicate record.

Is there an easy solution to this?
This simpliest solution is to move user away from insert script. After
inserting data you can use header() to send a "Location" header and
redirect user to another page (which can be even the same, but with an
empty form).

Of course, this is not bulletproof but should be enough.
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Jul 15 '06 #2

P: n/a
mindwarp wrote:
Hi,

When a user submits / posts data my php script Inserts data into my
database.
If they refresh the script or click back and click submit again I get
duplicate record.

Is there an easy solution to this?

Or do I need to create a hidden field which contains some sort of
unquie data, like a time stamp and have a have a unique field which
stops it adding the record?

Best regards,

Jules.

You could for instance:

a) test the values received against the database before inserting, i.e.
do a SELECT count(*) FROM table WHERE field1=$_POST[field1] AND
field2=$_POST[field2] ...

If the count if 1 or more, give an error message to the user and don't
insert.

b) make one or a combination of fields in your table unique which should
make the insert fail. Be careful which combination you choose.
Having a unique field in your table is always a good idea. Normally this
would be a record-id field which auto-increments. This however will not
prevent duplicate records from being entered as each record gets a new
unique auto-incremented id.

Having a timestamp as a unique field does not sound like a good idea.
What about two users opening the same form page at the exact same moment
? (depending on how busy your site is, probably a very rare occurance,
but still)
It could work if your site does not have large volumes of traffic, but
it is not a stable solution.

Good luck,
Juliette
Jul 16 '06 #3

P: n/a
mindwarp wrote:
Hi,

When a user submits / posts data my php script Inserts data into my
database.
If they refresh the script or click back and click submit again I get
duplicate record.

Is there an easy solution to this?

Or do I need to create a hidden field which contains some sort of
unquie data, like a time stamp and have a have a unique field which
stops it adding the record?

Best regards,

Jules.
You could also set a session variable indicating the record had been
added.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 16 '06 #4

P: n/a
"mindwarp" <mi*********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
Hi,

When a user submits / posts data my php script Inserts data into my
database.
If they refresh the script or click back and click submit again I get
duplicate record.

Is there an easy solution to this?

Or do I need to create a hidden field which contains some sort of
unquie data, like a time stamp and have a have a unique field which
stops it adding the record?
Generally I have a primary key and the "WHERE" clause prevents duplicates.
However, to avoid errors you could check first for its existence and the
either ignore it, send a message, or do an UPDATE.

Shelly
Jul 16 '06 #5

P: n/a
You can add an if to check the database if the has the record already.

$query_check = "SELECT * FROM `contest` WHERE record='$record'";
$query = @mysql_query($query_check) or die("Could not check for
account in our database.");

if (mysql_num_rows($query) 0) {
$rv echo "That record is already in the database";
}

Well something like that...

mindwarp wrote:
Hi,

When a user submits / posts data my php script Inserts data into my
database.
If they refresh the script or click back and click submit again I get
duplicate record.

Is there an easy solution to this?

Or do I need to create a hidden field which contains some sort of
unquie data, like a time stamp and have a have a unique field which
stops it adding the record?

Best regards,

Jules.
Jul 16 '06 #6

P: n/a
mindwarp wrote:
Hi,

When a user submits / posts data my php script Inserts data into my
database.
If they refresh the script or click back and click submit again I get
duplicate record.

Is there an easy solution to this?

Or do I need to create a hidden field which contains some sort of
unquie data, like a time stamp and have a have a unique field which
stops it adding the record?

Best regards,

Jules.
I would go with what Jerry said. Set a session variable indicating that
insertion has been done. If user tries to repost, check for existence of
the variable and just escape all the insertion code.
Jul 17 '06 #7

P: n/a
s a n j a y wrote:
mindwarp wrote:
>Hi,

When a user submits / posts data my php script Inserts data into my
database.
If they refresh the script or click back and click submit again I get
duplicate record.

Is there an easy solution to this?

Or do I need to create a hidden field which contains some sort of
unquie data, like a time stamp and have a have a unique field which
stops it adding the record?

Best regards,

Jules.

I would go with what Jerry said. Set a session variable indicating that
insertion has been done. If user tries to repost, check for existence of
the variable and just escape all the insertion code.

I wouldn't advice that as that would also exclude a user from posting
*something else* using the same form.
Jul 18 '06 #8

P: n/a
Juliette wrote:
s a n j a y wrote:
>mindwarp wrote:
>>Hi,

When a user submits / posts data my php script Inserts data into my
database.
If they refresh the script or click back and click submit again I get
duplicate record.

Is there an easy solution to this?

Or do I need to create a hidden field which contains some sort of
unquie data, like a time stamp and have a have a unique field which
stops it adding the record?

Best regards,

Jules.

I would go with what Jerry said. Set a session variable indicating
that insertion has been done. If user tries to repost, check for
existence of the variable and just escape all the insertion code.

I wouldn't advice that as that would also exclude a user from posting
*something else* using the same form.
Why? When the form is first displayed, clear the session variable.
Then when the form is submitted, set the variable.

Pretty standard programming.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 18 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.