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

stop duplicat entry in db

P: 3
Hello

I have a page on which i m placing order. After submitting the form, data is inserted to db successfully but when i refresh the page the duplicate data is inserted to db.

How can i stop this.

Can anybody help me?

Thanx in advance
Dec 13 '07 #1
Share this Question
Share on Google+
5 Replies


MarkoKlacar
Expert 100+
P: 296
Hi,

could you post your SQL query?
Dec 13 '07 #2

Markus
Expert 5K+
P: 6,050
Hello

I have a page on which i m placing order. After submitting the form, data is inserted to db successfully but when i refresh the page the duplicate data is inserted to db.

How can i stop this.

Can anybody help me?

Thanx in advance
[php]
//mysql_num_rows sounds like your savior!

//your mysql result
$query = "some_query";
$query = mysql_query($query);
$result = mysql_num_rows($query);
if($result > 1){
# data has already been inserted or page has been refreshed
# don't run any queries to insert data!
} else {
# data is unique
# run mysql queries to insert data
}
[/php]
Dec 13 '07 #3

P: 56
Hello
JUST TRY IT...

After inserting data execute the query and then write:

echo '<script language="javascript">document.location="'.basenam e($_SERVER['PHP_SELF']).'";</script>'."\n";
try this.
then reply me.
Dec 13 '07 #4

Markus
Expert 5K+
P: 6,050
Hello
JUST TRY IT...

After inserting data execute the query and then write:

echo '<script language="javascript">document.location="'.basenam e($_SERVER['PHP_SELF']).'";</script>'."\n";
try this.
then reply me.
not what he's looking for; the user could just press the back button and refresh the page from there.

And don't be so pushy.
Dec 13 '07 #5

100+
P: 111
The way that i stop this happening is by using the php redirect function.

create a flag, i.e. a variable that is picked up using $_get or $post. if this flag is set then insert the sqll query into the database.
after you have done the insert query succesfully, you can redirect to the same page again.

when this page is reloaded it will lose the $_REQUEST variables, that were sent in the initial page request first time.

therefore, the flag will not be set, and the insert query will not get executed


Even if the person presses the back button on their browser. they will not be able to reinsert the data into the database as the brower thinks it is at the same url becuase of the php redirect.

depening on whether you are using session variables, (I can't remember which way around it is)
you may have to redirect to another page and then from this page redirect back to the original page.
Dec 14 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.