Jump to content

Insert query is executed twice


Narel

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.