我有一个非常有用的系统。单击unfriend按钮后,表格将更新以删除您已更新的朋友。唯一的问题是它删除了你的所有朋友而不是数组中的1。
该数组类似于这个friend1,friend2,friend3,如果你想删除friend2,friend1和3也将无缘无故删除。
帮助:(
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 ...";
}
答案 0 :(得分:0)
首先,我建议您遵循@MarkBaker
和@nichar
建议并规范化您的数据库结构。您可以查看stackoverflow.com/questions/5135889/how-to-normalize-a-sql-database以及仅搜索stackoverflow - stackoverflow.com/search?q=How+to+normalize+a+SQL+Database。
考虑到这一点,以下是您的代码的一些问题 -
(1)您在两个查询中都获得了相同的好友数组。
//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']}'");
您的第二个查询应该是从他们正在删除的朋友那里获取朋友数组。
$add_friend_check_username = mysql_query("SELECT friend_array FROM users WHERE username='$username'");
(2)从朋友阵列中删除用户时,使用他们自己的朋友阵列
//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);
}
你应该从好友数组中删除它们
//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)完成从他们的朋友数组中删除它们的所有工作之后,你现在正在使朋友数组为空
$friend2 = "";
这应该删除。
(4)最后,您要更新用户朋友阵列两次,而不是朋友阵列,当您进行第二次更新时,您将根据问题#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']}'");
将您的第二个查询更改为 -
$removeFriendQuery_username = mysql_query("UPDATE users SET friend_array='$friend2' WHERE username='$username'");
现在,我假设
$username == the friend that you are deleting
$user == logged-in user || $_SESSION['user_login']
因为这些已被使用,但未在您的代码中定义。
另外,请注意,您不应该使用mysql_*
函数编写新代码。不鼓励使用此扩展名。相反,应使用MySQLi或PDO_MySQL扩展名。有关详细信息,另请参阅MySQL: choosing an API指南和相关FAQ。