我有一個不友好的系統,這種工作。點擊不友好按鈕後,表格會更新以刪除已更新的朋友。唯一的問題是,它會刪除所有的朋友,而不是數組中的1。更新聲明不會工作mysql
該數組類似於這個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 ...";
}
這就是爲什麼更多有經驗的開發人員規範化他們的數據庫結構 –
DB中的逗號分隔字符串並不好。可能值得看看'explode()'以在短期內更好地處理你的痛苦,然後考慮編寫一個腳本來規範化,而不是將新朋友附加到逗號列表中。 – nickhar