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(); ?> Link to comment https://forums.phpfreaks.com/topic/134925-solved-inserts-data-twice/ 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? Link to comment https://forums.phpfreaks.com/topic/134925-solved-inserts-data-twice/#findComment-702664 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? Link to comment https://forums.phpfreaks.com/topic/134925-solved-inserts-data-twice/#findComment-702665 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. Link to comment https://forums.phpfreaks.com/topic/134925-solved-inserts-data-twice/#findComment-702668 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. Link to comment https://forums.phpfreaks.com/topic/134925-solved-inserts-data-twice/#findComment-702669 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? Link to comment https://forums.phpfreaks.com/topic/134925-solved-inserts-data-twice/#findComment-702670 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. Link to comment https://forums.phpfreaks.com/topic/134925-solved-inserts-data-twice/#findComment-702683 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 Link to comment https://forums.phpfreaks.com/topic/134925-solved-inserts-data-twice/#findComment-702684 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. Link to comment https://forums.phpfreaks.com/topic/134925-solved-inserts-data-twice/#findComment-702690 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 Link to comment https://forums.phpfreaks.com/topic/134925-solved-inserts-data-twice/#findComment-702693 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. Link to comment https://forums.phpfreaks.com/topic/134925-solved-inserts-data-twice/#findComment-702694 Share on other sites More sharing options...
peranha Posted December 1, 2008 Author Share Posted December 1, 2008 Nope, no firebug Link to comment https://forums.phpfreaks.com/topic/134925-solved-inserts-data-twice/#findComment-702695 Share on other sites More sharing options...
Prismatic Posted December 1, 2008 Share Posted December 1, 2008 Are you double clicking your links? Link to comment https://forums.phpfreaks.com/topic/134925-solved-inserts-data-twice/#findComment-702699 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. Link to comment https://forums.phpfreaks.com/topic/134925-solved-inserts-data-twice/#findComment-702705 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.