Narel Posted March 31, 2009 Share Posted March 31, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151954-insert-query-is-executed-twice/ Share on other sites More sharing options...
wildteen88 Posted March 31, 2009 Share Posted March 31, 2009 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(); } Quote Link to comment https://forums.phpfreaks.com/topic/151954-insert-query-is-executed-twice/#findComment-797958 Share on other sites More sharing options...
Narel Posted March 31, 2009 Author Share Posted March 31, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/151954-insert-query-is-executed-twice/#findComment-797966 Share on other sites More sharing options...
wildteen88 Posted March 31, 2009 Share Posted March 31, 2009 You sure you don't call the function anywhere else within your script? Quote Link to comment https://forums.phpfreaks.com/topic/151954-insert-query-is-executed-twice/#findComment-797969 Share on other sites More sharing options...
Narel Posted March 31, 2009 Author Share Posted March 31, 2009 Yes, the only way the add_friend() is called is by checking the ref in the get_ref($ref) function in one line. Quote Link to comment https://forums.phpfreaks.com/topic/151954-insert-query-is-executed-twice/#findComment-797989 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.