I have an unfriend system which sort of works. When the unfriend button is clicked, the table is updated to remove the friend you have updated. The only problem being that it deletes all your friends instead of just 1 in an array.
The array is similar to this friend1,friend2,friend3 and if u wish to delete friend2, friend1 and 3 will also be deleted for no reason.
Help :(
if (@$_POST['removefriend']) {
//Friend array for logged in user
$add_friend_check = mysql_query("SELECT friend_array FROM users WHERE username='{$_SESSION['user_login']}'");
$get_friend_row = mysql_fetch_assoc($add_friend_check);
$friend_array = $get_friend_row['friend_array'];
$friend_array_explode = explode(",",$friend_array);
$friend_array_count = count($friend_array_explode);
//Friend array for user who owns profile
$add_friend_check_username = mysql_query("SELECT friend_array FROM users WHERE username='{$_SESSION['user_login']}'");
$get_friend_row_username = mysql_fetch_assoc($add_friend_check_username);
$friend_array_username = $get_friend_row_username['friend_array'];
$friend_array_explode_username = explode(",",$friend_array_username);
$friend_array_count_username = count($friend_array_explode_username);
$usernameComma = ",".$username;
$usernameComma2 = $username.",";
$userComma = ",".$user;
$userComma2 = $user.",";
if (strstr($friend_array,$usernameComma)) {
$friend1 = str_replace("$usernameComma","",$friend_array);
}
else
if (strstr($friend_array,$usernameComma2)) {
$friend1 = str_replace("$usernameComma2","",$friend_array);
}
else
if (strstr($friend_array,$username)) {
$friend1 = str_replace("$username","",$friend_array);
}
//Remove logged in user from other persons array
if (strstr($friend_array,$userComma)) {
$friend2 = str_replace("$userComma","",$friend_array);
}
else
if (strstr($friend_array,$userComma2)) {
$friend2 = str_replace("$userComma2","",$friend_array);
}
else
if (strstr($friend_array,$user)) {
$friend2 = str_replace("$user","",$friend_array);
}
$friend2 = "";
$removeFriendQuery = mysql_query("UPDATE users SET friend_array='$friend1' WHERE username='{$_SESSION['user_login']}'");
$removeFriendQuery_username = mysql_query("UPDATE users SET friend_array='$friend2' WHERE username='{$_SESSION['user_login']}'");
echo "Friend Removed ...";
}
First I recommend following what @MarkBaker
and @nichar
suggest and normalize your database structure. You can look at stackoverflow.com/questions/5135889/how-to-normalize-a-sql-database as well as just searching stackoverflow -stackoverflow.com/search?q=How+to+normalize+a+SQL+Database.
With that in mind, here are some issues with your code-
(1) You are getting the same friend array in both queries.
//Friend array for logged in user
$add_friend_check = mysql_query("SELECT friend_array FROM users WHERE username='{$_SESSION['user_login']}'");
//Friend array for user who owns profile
$add_friend_check_username = mysql_query("SELECT friend_array FROM users WHERE username='{$_SESSION['user_login']}'");
Your second query should be getting the friend array from the friend they are deleting.
$add_friend_check_username = mysql_query("SELECT friend_array FROM users WHERE username='$username'");
(2) When removing the user from their friends array you using their own friend array
//Remove logged in user from other persons array
if (strstr($friend_array,$userComma)) {
$friend2 = str_replace("$userComma","",$friend_array);
}
else
if (strstr($friend_array,$userComma2)) {
$friend2 = str_replace("$userComma2","",$friend_array);
}
else
if (strstr($friend_array,$user)) {
$friend2 = str_replace("$user","",$friend_array);
}
You should be removing them from the friends array
//Remove logged in user from other persons array
if (strstr($friend_array_username,$userComma)) {
$friend2 = str_replace("$userComma","",$friend_array_username);
}
else
if (strstr($friend_array_username,$userComma2)) {
$friend2 = str_replace("$userComma2","",$friend_array_username);
}
else
if (strstr($friend_array_username,$user)) {
$friend2 = str_replace("$user","",$friend_array_username);
}
(3) After all the work of just removing them from their friends array, you now are making the friend array to be empty
$friend2 = "";
This should be removed.
(4) Finally, you are updating the users friend array twice, not the friends array, and when you do the second update you are making it empty based off issue #4
$removeFriendQuery = mysql_query("UPDATE users SET friend_array='$friend1' WHERE username='{$_SESSION['user_login']}'");
$removeFriendQuery_username = mysql_query("UPDATE users SET friend_array='$friend2' WHERE username='{$_SESSION['user_login']}'");
Change your second query to -
$removeFriendQuery_username = mysql_query("UPDATE users SET friend_array='$friend2' WHERE username='$username'");
Now, I am assuming that
$username == the friend that you are deleting
$user == logged-in user || $_SESSION['user_login']
since these are used, but not defined in your code.
Also, be aware that you should not be writing new code with mysql_*
functions. Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.