我有一个系统,用户的朋友存储在与该用户关联的阵列中。我有一个函数,只能从这个朋友数组中删除1个人。但是,当从remove friend
按钮触发此代码时,它会删除整个数组。我将数组设置为用逗号分隔用户(因此各种$usernameComma
变量)
if (@$_POST['removefriend']) {
//Friend array for logged in user
$add_friend_check = mysql_query("SELECT friend_array FROM users WHERE username='$user'");
$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='$username'");
$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='$user'");
$removeFriendQuery_username = mysql_query("UPDATE users SET friend_array='$friend2' WHERE username='$username'");
echo "Friend Removed ...";
}
?>
答案 0 :(得分:0)
看起来你总是通过在查询之前设置$ friend2 =“”来消除第二个用户(“其他人数组”)的friend_array。
但是,请务必遵循Mike Brant的评论中的建议。这是一个更好,更好的方法。改变可能需要做更多的工作,但最终结果对于今后的发展将更有用。