Millica
Millica

Reputation: 168

Can't display avatar - PHP, MySQL

I've created a private message system and I'm trying to show the avatar for the person who receives the message, but instead of getting the avatar for the receiver, it's trying to get the image for the logged in user.

For the image source, avatars are stored in a folder called user. The layout of the folder is user/username/avatar. However, when implemented, the correct user folder is opened, but it searches for the avatar of the logged in user.

Also, one more thing to note. For some reason the avatar of the sender (parent message) is shown, but not the receiver.

Here's my PHP.

$sql = "
    SELECT * FROM users 
    INNER JOIN pm ON users.username = pm.sender 
    WHERE (
        receiver='$logged_in_user' 
        AND parent='x' AND rdelete='0'
    ) 
    OR (
        sender='$logged_in_user' 
        AND sdelete='0' AND parent='x'
    )
    ORDER BY senttime DESC";
$query = mysqli_query($db, $sql);
$statusnumrows = mysqli_num_rows($query);

if($statusnumrows > 0){
    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
        //assigning variables
        $avatar = $row["avatar"];
        $sender = $row["sender"];
        $receiver = $row["receiver"];

        if($parent == "x" && $sender == $logged_in_user){
           $profile_pic = '<img src="user/'.$receiver.'/'.$avatar.'" alt="'.$receiver.'">';
        }
     }
}
echo $profile_pic;

Upvotes: 1

Views: 191

Answers (1)

Kevin_Kinsey
Kevin_Kinsey

Reputation: 2300

So, this PHP is supposed to yield the receiver's avatar?

Look at the SQL here:

INNER JOIN pm ON users.username = pm.sender 

Try instead:

INNER JOIN pm ON users.username = pm.receiver

(assuming of course the column is named "receiver" in your schema).

Upvotes: 3

Related Questions