hellrising Posted April 6, 2010 Share Posted April 6, 2010 Ok here is another stupid problem, a simple add two records to two seperate tables, but it duplicates the record (inserts two the same) but with different ID's that are auto_incresments... $mesexicute = mysql_query("INSERT INTO Messages (From, To, Message, Date, Notification) VALUES ('$Friendrequesting','$Profileowner','$notimessage',NOW(),'true')"); if (!$mesexicute) { die('Could not successfully run query ($link) from DB: ' . mysql_error()); exit; } $inf = mysql_query("INSERT INTO FansFriendList (ID, profilename, fanfriend, rel_type, approved_fanfriend) VALUES (NULL, '$Profileowner','$Friendrequesting','friend','false')"); if (!$inf) { die('Could not successfully run query ($link) from DB: ' . mysql_error()); exit; } So let me try and explain this... it works fine... inserts both my records into each table...but there is TWO the same in each table... is it exicuting it twice.... FULL CODE: $mesexicute = mysql_query("INSERT INTO Messages (From, To, Message, Date, Notification) VALUES ('$Friendrequesting','$Profileowner','$notimessage',NOW(),'true')"); if (!$mesexicute) { die('Could not successfully run query ($link) from DB: ' . mysql_error()); exit; } $inf = mysql_query("INSERT INTO FansFriendList (ID, profilename, fanfriend, rel_type, approved_fanfriend) VALUES (NULL, '$Profileowner','$Friendrequesting','friend','false')"); if (!$inf) { die('Could not successfully run query ($link) from DB: ' . mysql_error()); exit; } header ("LOCATION: profile.php"); // Send back to profile this is a different page, so its not revisiting the same page, but why two records header ("LOCATION: profile.php"); // Send back to profile Quote Link to comment https://forums.phpfreaks.com/topic/197697-duplicating-unwanted-records/ Share on other sites More sharing options...
MatthewJ Posted April 6, 2010 Share Posted April 6, 2010 The code isn't inside a loop that you didn't post right? Also, the word is exEcute Quote Link to comment https://forums.phpfreaks.com/topic/197697-duplicating-unwanted-records/#findComment-1037524 Share on other sites More sharing options...
hellrising Posted April 6, 2010 Author Share Posted April 6, 2010 The code isn't inside a loop that you didn't post right? Also, the word is exEcute The code is NOT in a loop 'correct' and thanks for the typo... It just simply executes the page then leaves, but inserts two records that are the same, and it is really getting to me...lol Quote Link to comment https://forums.phpfreaks.com/topic/197697-duplicating-unwanted-records/#findComment-1037526 Share on other sites More sharing options...
PFMaBiSmAd Posted April 6, 2010 Share Posted April 6, 2010 If the data is present in both sets of records being inserted, it is likely that your form is submitting twice. Do you have an javascript that is submitting the form (along with the browser's normal form submission)? What is your form? Also, what is the whole code on the page, because you may have other queries being executed on that page that are causing the problem (I notice that you don't have an exit statement after the header() redirect so any further code on that page is still being executed while the browser performs the redirect.) Quote Link to comment https://forums.phpfreaks.com/topic/197697-duplicating-unwanted-records/#findComment-1037528 Share on other sites More sharing options...
hellrising Posted April 6, 2010 Author Share Posted April 6, 2010 No form, No javascript, just a link with querys in it... <br /> <p>Become <?PHP echo $DisplayProfile ?>'s friend, click the button below to send <?PHP echo $DisplayProfile ?> a friend request.</p><br /> <br /><p align="center"><a href="friendfunctions.php?friendrequest=<?PHP echo $DisplayProfile ?>&friend=<?PHP echo $sessionuser ?>"><button class="close"> send <?PHP echo $DisplayProfile ?> a friend request </button></a></p><br /> this then executes this when it is clicked... $Profileowner = $_REQUEST['friendrequest']; $Friendrequesting = $_REQUEST['friend']; $notimessage = "You have recieved a friend request from ".$Friendrequesting.".<br>To accept this request click on Accept, or remove the request by clicking Decline<br><p align=center><a href=friendfunctions.php?acceptfriendrequest=".$Friendrequesting."&friend=".$Profileowner."><button class=close> Accept </button></a> <a href=profilefunctions.php?declinefriendrequest=".$Friendrequesting."&friend=".$Profileowner."><button class=close> Decline </button></a></p>"; $mesexicute = mysql_query("INSERT INTO Messages (From, To, Message, Date, Notification) VALUES ('$Friendrequesting','$Profileowner','$notimessage',NOW(),'true')"); if (!$mesexicute) { die('Could not successfully run query ($link) from DB: ' . mysql_error()); exit; } $inf = mysql_query("INSERT INTO FansFriendList (ID, profilename, fanfriend, rel_type, approved_fanfriend) VALUES (NULL, '$Profileowner','$Friendrequesting','friend','false')"); if (!$inf) { die('Could not successfully run query ($link) from DB: ' . mysql_error()); exit; } header ("LOCATION: profile.php"); // Send back to profile Quote Link to comment https://forums.phpfreaks.com/topic/197697-duplicating-unwanted-records/#findComment-1037533 Share on other sites More sharing options...
hellrising Posted April 6, 2010 Author Share Posted April 6, 2010 (I notice that you don't have an exit statement after the header() redirect so any further code on that page is still being executed while the browser performs the redirect.) i did not know this... Quote Link to comment https://forums.phpfreaks.com/topic/197697-duplicating-unwanted-records/#findComment-1037535 Share on other sites More sharing options...
hellrising Posted April 6, 2010 Author Share Posted April 6, 2010 But even after I have added exit; after the header function, it is still duplicating, and the other code on the page is in a ' if (){} ' clause anyway... Quote Link to comment https://forums.phpfreaks.com/topic/197697-duplicating-unwanted-records/#findComment-1037537 Share on other sites More sharing options...
hellrising Posted April 6, 2010 Author Share Posted April 6, 2010 But even after I have added exit; after the header function, it is still duplicating, and the other code on the page is in a ' if (){} ' clause anyway... And it still does it if i remove all other code.... it has stumped me... Quote Link to comment https://forums.phpfreaks.com/topic/197697-duplicating-unwanted-records/#findComment-1037541 Share on other sites More sharing options...
PFMaBiSmAd Posted April 6, 2010 Share Posted April 6, 2010 Since the data is contained in the URL, the problem is because the page is being requested twice and it receives the data twice. Different browsers do this for different reasons and some url rewriting and even how hosts setup accounts can cause this. Most of the causes for a double page request are out of your hands, so the best method to prevent this is to detect and prevent duplicate data entry. If you can, setup unique indexes in your table(s) so that duplicate data will be prevented. You can also set a session variable to prevent the page from processing more than one time. Quote Link to comment https://forums.phpfreaks.com/topic/197697-duplicating-unwanted-records/#findComment-1037544 Share on other sites More sharing options...
hellrising Posted April 6, 2010 Author Share Posted April 6, 2010 $_session good idea... or maybe cookie...cheers anyway, strange how other scripts i have useed in the same way are not doing this... Quote Link to comment https://forums.phpfreaks.com/topic/197697-duplicating-unwanted-records/#findComment-1037545 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.