Marty Lavender
Marty Lavender

Reputation: 105

Unable to display default image if no image has been uploaded

I am wanting to display a default image when a user creates a new account as they will of course not have uploaded an avatar yet. Below is what I have come up with but it does not seem to be working:

<?php
    $getImage = $pdo->prepare("SELECT * FROM photos WHERE userID = '" . $_SESSION['user_id'] . "'");
    $getImage->bindParam(':userID',$_SESSION['user_id']);
    $getImage->execute();

    while ($row = $getImage->fetch(PDO::FETCH_ASSOC)) {
        $image_name = $row["imagePath"];

        if(file_exists($image_name )) {
            $userAvatar = $image_name;
        } else {
            $userAvatar = 'uploads/default.jpg';
        }
    }
?>

Of course if I do upload an image, that image is displayed just fine. Any help would be appreciated.

Upvotes: 0

Views: 32

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94662

Your query is wrong. You are mixing data concatenation and parameterised and bound queries. Parameterised and bound queries are better.

<?php
    $getImage = $pdo->prepare("SELECT * FROM photos WHERE userID = :userID");
    $getImage->bindParam(':userID',$_SESSION['user_id']);
    $getImage->execute();

Upvotes: 1

Related Questions