473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Duplicate records added from post forms, easy solutions?

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
8 2680
*** 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
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
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: tom | last post by:
I am new to SQL administration. >From a list of IDs that are the primary key in one table (i.e. Customer Table), I want to make changes in tables that use those IDs as a foreign key. ...
8
by: Mark | last post by:
When my form goes to a new record, I have a procedure that copies the last record added to the form's underlying table into the form. The intent is that a series of new records may have the same...
3
by: Nhmiller | last post by:
I searched here for an answer. I am entering records into a database, and it would save a lot of time if I could duplicate a record that is very similar to the new one I am about to enter, then...
2
by: Carroll | last post by:
I'm looking for a way in SQL to find duplicate records in a single table, that are the same based on 3 columns, regardless of what is in the other columns in the duplicate records. I would like to...
9
by: Tom_F | last post by:
To comp.databases.ms-access -- I just discovered, to my more than mild dismay, that some tables in my Microsoft Access 2003 database have duplicate numbers in the "AutoNumber" field. (Field...
4
theaybaras
by: theaybaras | last post by:
Hi All, I am creating a databse of articles and related information. I need to find a way to prevent the user from inputting duplicate information in a few forms. In one of these forms I can...
2
by: altesse33 | last post by:
I have a command button on the main form of my database that allows users to add new records. But, even though new records get added to the sub table when I click on the button, those new records...
1
by: VinArt | last post by:
MS Acc 2003, XP Thank you in advance for any help. I have tables called "Makeup" and "Lines". Each makeup can have multiple lines. Goal is to create a new "makeup" with identical "lines"...
4
by: Thomas Arthur Seidel | last post by:
Hello to all, I have a small or big problem with a customer data base, where during a change of system we might have created duplicate records. This should be easy to find, you might think, but,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.