Jump to content

Recommended Posts

Hi Guys,

 

i htought i had this query pretty much down , but it's not working, basically i want to check if a logged in user has alreaady added someone as a friend or not if so "you have already added this user as a friend" else enter the users info into mysql

 

code:

 

<?php 
     // add to friends...////////////////////////////////////////////////////////////////
     $friend_to_adds_id = $_GET['id'];
     
     // the logged in users id...////////////////////////////////////////////////////////
     $query1 = "SELECT `id` FROM `membership` WHERE `username`='$member'";
     $result1 = mysql_query($query1) or die (mysql_error());
     $row = mysql_fetch_array($result1) or die (mysql_error());
     
     // get the logged in users id in a variable...//////////////////////////////////////
     $logged_in_users_id = $row['id'];
     
     // see if the friend has already been added...//////////////////////////////////////
     $query0 = "SELECT `friend_id` FROM `friends` WHERE `friend_id`='$friend_to_adds_id'";
     $result0 = mysql_query($query0) or die (mysql_error());
     
     if ($result0 == 0) {
     
     // all good? lets insert it into the database...////////////////////////////////////
     $query2 = "INSERT INTO `friends` (`user_id`,`friend_id`) VALUES ('$friend_to_adds_id','$logged_in_users_id')";
     $result2 = mysql_query($query2) or die (mysql_error()); 
     
     if ($result2) {
     
          echo "<br /><b>Friend Added Successfully! You Can Remove Them Via The My Account Page.</b><br /><br />";
     
     }
          
     } else {
     
          echo "<br/><b>You Have Already Added This User As A Friend!</b><br /><br />"; 
     
     } ## end else...////////////////////////////////////////////////////////////////////
?>

 

once i delete the entries from mysql (for testing) it always says "You Have Already Added This User As A Friend!"

 

any help would be great

 

cheers

 

Graham

 

Link to comment
https://forums.phpfreaks.com/topic/51723-proble-with-an-ifelse/
Share on other sites

<?php 
     // add to friends...////////////////////////////////////////////////////////////////
     $friend_to_adds_id = $_GET['id'];
     
     // the logged in users id...////////////////////////////////////////////////////////
     $query1 = "SELECT `id` FROM `membership` WHERE `username`='$member'";
     $result1 = mysql_query($query1) or die (mysql_error());
     $row = mysql_fetch_array($result1) or die (mysql_error());
     
     // get the logged in users id in a variable...//////////////////////////////////////
     $logged_in_users_id = $row['id'];
     
     // see if the friend has already been added...//////////////////////////////////////
     $query0 = "SELECT `friend_id` FROM `friends` WHERE `friend_id`='$friend_to_adds_id'";
     $result0 = mysql_query($query0) or die (mysql_error());
     
     if (!$result0) {
     
     // all good? lets insert it into the database...////////////////////////////////////
     $query2 = "INSERT INTO `friends` (`user_id`,`friend_id`) VALUES ('$friend_to_adds_id','$logged_in_users_id')";
     $result2 = mysql_query($query2) or die (mysql_error()); 
     
     if ($result2) {
     
          echo "<br /><b>Friend Added Successfully! You Can Remove Them Via The My Account Page.</b><br /><br />";
     
     }
          
     } else {
     
          echo "<br/><b>You Have Already Added This User As A Friend!</b><br /><br />"; 
     
     } ## end else...////////////////////////////////////////////////////////////////////
?>

 

Try that?

Link to comment
https://forums.phpfreaks.com/topic/51723-proble-with-an-ifelse/#findComment-254778
Share on other sites

Your first query ($query0) only checks on friend_id. That would mean that everyone can only have 1 friend!

 

 
$query0 = "SELECT `friend_id` FROM `friends` WHERE friend_id`='$friend_to_adds_id'";
// SHOULD BE
$query0 = "SELECT `friend_id` FROM `friends`
                WHERE friend_id`='$friend_to_adds_id' AND user_id = '$logged_in_users_id'";
// BUT REALLY MAY NEED TO BE
$query0 = "SELECT `friend_id` FROM `friends`
                WHERE (friend_id`='$friend_to_adds_id' AND user_id = '$logged_in_users_id')
                OR (friend_id`='$logged_in_users_id' AND user_id = '$friend_to_adds_id')";

 

If you used the middle query above, then the order of your INSERT appears to be backwards as well:

$query2 = "INSERT INTO `friends` (`user_id`,`friend_id`) VALUES ('$friend_to_adds_id','$logged_in_users_id')";
// SHOULD BE
$query2 = "INSERT INTO `friends` (`user_id`,`friend_id`) VALUES ('$logged_in_users_id','$friend_to_adds_id')";

 

You could bypass the whole issue by creating a unique index on user_id and friend_id, and use the following:

 

// the friend with the lowest id will be stored in user_id, and the other stored in friend_id
// ie: user_id = 1, friend_id = 2
$friends = array($logged_in_users_id, $friends_to_adds_id);
asort($friends);
$query2 = "REPLACE INTO `friends` (`user_id`, `friend_id`) VALUES ('{$friends[0]}', '{$friends[1]}'";

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/51723-proble-with-an-ifelse/#findComment-254953
Share on other sites

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.