2012-11-20 85 views
0

我有一個不友好的系統,這種工作。點擊不友好按鈕後,表格會更新以刪除已更新的朋友。唯一的問題是,它會刪除所有的朋友,而不是數組中的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 ..."; 
     } 
+4

這就是爲什麼更多有經驗的開發人員規範化他們的數據庫結構 –

+0

DB中的逗號分隔字符串並不好。可能值得看看'explode()'以在短期內更好地處理你的痛苦,然後考慮編寫一個腳本來規範化,而不是將新朋友附加到逗號列表中。 – nickhar

回答

0

首先,我建議以下什麼@MarkBaker@nichar建議和規範你的數據庫結構,你可以看一下stackoverflow.com/questions/5135889/how-to-normalize-a-sql-database以及剛剛搜索計算器 - 。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_*函數編寫新代碼。不鼓勵使用此擴展名。應該使用MySQLiPDO_MySQL擴展名。有關更多信息,另請參閱MySQL: choosing an API指南和相關FAQ

+0

另外,你是否知道在逗號分隔的數組上使用'strstr()'&'str_replace()'的問題?例如,如果您要刪除'friend1','strstr()'&'str_replace()'會匹配並從friend111,friend143和其他像friend1 ...中移除friend1。到你的'if/elseif/elseif'語句。 – Sean