peranha Posted December 1, 2008 Share Posted December 1, 2008 I have the following code on a page, and this is inserting data twice to the sql database, and I cannot figure out why. If I execute it in myadmin, it only inserts 1. <?php // Create query and execute query. $bquery = mysql_query("INSERT INTO " . $pregm . "positions (position_1, position_2, position_3) VALUES ('{$position1}', '{$position2}', '{$position3}')"); // redirect back to the index page. header ("Location: index.php"); exit(); ?> Quote Link to comment Share on other sites More sharing options...
waynew Posted December 1, 2008 Share Posted December 1, 2008 Are you sure that it's inserting it twice? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 1, 2008 Share Posted December 1, 2008 There is no reason the code you posted would insert twice. How are you calling the code? Is a page submitting to it? Quote Link to comment Share on other sites More sharing options...
peranha Posted December 1, 2008 Author Share Posted December 1, 2008 Are you sure that it's inserting it twice? Positive it is There is no reason the code you posted would insert twice. How are you calling the code? Is a page submitting to it? I am calling if from a hyperlink. That is the complete page, minus the database connection, and selecting the database. Quote Link to comment Share on other sites More sharing options...
waynew Posted December 1, 2008 Share Posted December 1, 2008 All I can say is that something else in your application is causing it to happen. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 1, 2008 Share Posted December 1, 2008 if you comment out the header() line, and just do an echo "DONE"; does it still enter twice? Quote Link to comment Share on other sites More sharing options...
peranha Posted December 1, 2008 Author Share Posted December 1, 2008 Yes it does, it is something with the hyperlink that is causing it to happen, if I call the page directly, it only inserts once. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 1, 2008 Share Posted December 1, 2008 Your browser is requesting the page twice. You need to add some logic to prevent the requests after the first one from executing the database query - http://www.phpfreaks.com/forums/index.php/topic,226301.msg1043443.html#msg1043443 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted December 1, 2008 Share Posted December 1, 2008 Moral of the story: Specify a charset for your page. Quote Link to comment Share on other sites More sharing options...
peranha Posted December 1, 2008 Author Share Posted December 1, 2008 charset is set to utf-8 I will look into this. Thanks. Your browser is requesting the page twice. You need to add some logic to prevent the requests after the first one from executing the database query - http://www.phpfreaks.com/forums/index.php/topic,226301.msg1043443.html#msg1043443 Quote Link to comment Share on other sites More sharing options...
awpti Posted December 1, 2008 Share Posted December 1, 2008 I bet you have Firebug. Turn it off. Quote Link to comment Share on other sites More sharing options...
peranha Posted December 1, 2008 Author Share Posted December 1, 2008 Nope, no firebug Quote Link to comment Share on other sites More sharing options...
Prismatic Posted December 1, 2008 Share Posted December 1, 2008 Are you double clicking your links? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 1, 2008 Share Posted December 1, 2008 There are several reasons a page can get requested twice - browsers, url rewriting, javascript form submission... The fix is to detect multiple page requests and ignore all but the first one. The code in the first post is not even doing the minimum validation needed to prevent a search engine indexing the site from inserting empty records. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.