graham23s Posted May 16, 2007 Share Posted May 16, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/51723-proble-with-an-ifelse/ Share on other sites More sharing options...
chigley Posted May 16, 2007 Share Posted May 16, 2007 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/51723-proble-with-an-ifelse/#findComment-254778 Share on other sites More sharing options...
graham23s Posted May 16, 2007 Author Share Posted May 16, 2007 Hi Mate, just tried the code it's still the same (nothing in mysql) but it says "You Have Already Added This User As A Friend!", crazy the code looks pretty straight forward aswell. Graham Quote Link to comment https://forums.phpfreaks.com/topic/51723-proble-with-an-ifelse/#findComment-254792 Share on other sites More sharing options...
cooldude832 Posted May 16, 2007 Share Posted May 16, 2007 could you possibly be looking for a blank string?? Quote Link to comment https://forums.phpfreaks.com/topic/51723-proble-with-an-ifelse/#findComment-254798 Share on other sites More sharing options...
lee20 Posted May 17, 2007 Share Posted May 17, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/51723-proble-with-an-ifelse/#findComment-254953 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.