Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/197697-duplicating-unwanted-records/
Share on other sites

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

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.)

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.