Search code examples
phpmysqlarrayssyntax-errorstrstr

strstr() in friend array not working


I have this code, which takes data from MySQL of two users. And when i click on "remove" button it will erase the user from both friend array and results return back into database. But it doesn't work. Help me, please.

if (@$_POST['removefriend']) {
    $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);

    $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);
    } elseif (strstr($friend_array, $usernameComma2)) {
        $friend1 = str_replace("$usernameComma2", "", $friend_array);
    } elseif (strstr($friend_array, $username)) {
        $friend1 = str_replace("$username", "", $friend_array);
    }

    if (strstr($friend_array, $userComma)) {
        $friend2 = str_replace("$userComma", "", $friend_array);
    } elseif (strstr($friend_array, $userComma2)) {
        $friend2 = str_replace("$userComma2", "", $friend_array);
    } elseif (strstr($friend_array, $user)) {
        $friend2 = str_replace("$user", "", $friend_array);
    }

    $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 was deleted...";
    header("Location: /$username");

Solution

  • You can simplify a lot your code this way:

    http://sqlfiddle.com/#!9/01c49/1

    //somewhere prepare dbo connection like:
    $dbh = new PDO("pgsql:host=$host;port=5432;dbname=$db;user=$user;password=$pass");
    
    if (@$_POST['removefriend']) {
    
    $sth = $dbh->prepare("UPDATE users 
    SET users.friend_array=
    CASE WHEN FIND_IN_SET( :friend ,users.friend_array) > 1 THEN
       REPLACE(users.friend_array , CONCAT(',',:friend),'')
    WHEN TRIM(users.friend_array) = :friend THEN
       ''
    ELSE REPLACE(users.friend_array , CONCAT(:friend,',','') END
    WHERE username= :username 
       AND FIND_IN_SET( :friend ,users.friend_array) >0");
    $sth->bindValue(':username', $user, PDO::PARAM_STR);
    $sth->bindValue(':friend', $username, PDO::PARAM_STR);
    $sth->execute();
    
    $sth->bindValue(':username', $username, PDO::PARAM_STR);
    $sth->bindValue(':friend', $user, PDO::PARAM_STR);
    $sth->execute();
    
    header("Location: /$username");
    

    But as @MarcB said it is better to redesign your database.