Jump to content

Recommended Posts

I have a simple link that by clicking it, its supposed to INSERT in the Database some data (what else). In fact, it takes the member id (mId) from the URL and insert a new row in the Table Contacts using the $_SESSION['id'] to determine who is the member that make the friendship request. The problem is that for some reason the query is executed twice.

The code of the link  is here (without any url checks):

<a href=\"home.php?mod=friends&ref=addfriend&mid=".$row['mId']."\">Add Friend</a>

The code of the function which is called when the link is clicked is the following:

function add_friend() {
	$connection = db_connection();
	$id = $_SESSION['id'];
	$friendId = $_GET['mid'];
	$query = "INSERT INTO Contacts (cFromMid, cToMid, confirmDate, confirmStatus) VALUES ('".$id."', '".$friendId."', '0000-00-00 00:00:00', 'n');";
	$result = mysqli_query($connection, $query);
	confirm_query($result);
}

The Table Contacts is this:

Contacts (cId, cFromMid, cToMid, requestDate, confirmDate, confirmStatus)

I have checked my $_GET['actions'], $_GET['ref'] functions and none of them seems to call the add_friend() function twice.

Link to comment
https://forums.phpfreaks.com/topic/151954-insert-query-is-executed-twice/
Share on other sites

Where do you call the add_friend() function?

 

If you call the function add_friend() without checking whether $_GET['ref'] is set to 'addfriend' and $_GET['mid'] is present. Then your function will always be called and executed regardless of the url.

 

You should be calling your function like so

 

if(isset($_GET['ref'], $_GET['mid']) && $_GET['ref'] == 'addfriend' && is_numeric($_GET['mid']))
{
    add_friend();
}

I call the add_friend() function in the get_ref($ref) function which checks all the refs to determine which functions to be called. The add_friend() can be called only if i click the specific link and if the $_GET['ref'] == 'addfriend' so i dont know what is the problem. I try what you suggested but didnt work. Thank you , although.

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.