jeudi 17 novembre 2016

Expanding on SELECT query

I created a query that is checking for "friend relations". Basically, friend_one/$user_id is the user signed in and friend_two/$profile_user is the other user's id... ie: If I clicked on another user's profile on here, it is that user.

My system works fine, except for when the logged in user looks at their own profile. Since my query is checking for the logged in user viewing another profile, it throws undefined variable errors when the logged in user views their own profile.

Is there anything I can do to my SQL or php that I can change so when friend_one / user_id views there own profile that it doesn't throw unrecognized variable errors?

The first unrecognized variable error starts here: if ($friend_status == 2) {. So the query doesn't throw an error.

Errors are showing because I will never have a user have relation with themselves in this table. So, I guess this error will occur when viewing "unfriended" profiles as well. Any ideas of a way to fix it?


EDIT:

My original answer was a little hard to understand. Basically the only time a friend "relation" or insertion into my database is made is if a user friends another user. So, if this relation is not in the database and in my query, I want to "let it go", so it doesn't just check for friend_one and friend_two relation.

$okay = true;

//Checks 
if ( $okay ) {
    $friend_sql = "
        SELECT *
        FROM friends
        WHERE friend_one = ?
        AND friend_two = ?
    ";
    $select_friend_stmt = $con->prepare($friend_sql);
    $select_friend_stmt->execute(array($user_id, $profile_user));
    $friend_rows = $select_friend_stmt->fetchAll(PDO::FETCH_ASSOC);
    foreach ($friend_rows as $friend_row) {
        $select_friend_1 = $friend_row['friend_one'];
        $select_friend_2 = $friend_row['friend_two'];
        $friend_status = $friend_row['status'];
        $friend_status_date = $friend_row['date'];
    }
}
else {
    echo "Friend Status not found.";
}
$friend_status_button = null;
if ($friend_status == 2) {
    $friend_status_button = "Approved";
}
else if ($friend_status == 1) {
        $friend_status_button = "Pending";
}
else if ($select_friend_1 == $select_friend_2) {
    $friend_status_button = null;
}
else {
    $friend_status_button = "Add Friend";
}

Aucun commentaire:

Enregistrer un commentaire