Debraz G
Debraz G

Reputation: 13

php check whether the user has uploaded a profile pic or not

Hello how to check whether the user has uploaded profile pic or not...I have tried this code but only displays the default pic and not the else part..please help...this is my code

$check_pic = mysqli_query($conn,"SELECT image FROM users WHERE id='$id'");
$get_pic_row = mysqli_fetch_assoc($check_pic);
$profile_pic_db = $get_pic_row['image'];
$pro_num = mysqli_num_rows($profile_pic_db);
if ($pro_num == 0) {
    $profile_pic = "http://localhost/Ramdhenu/images/default_propic.png";
} else {
    $profile_pic = "http://localhost/Ramdhenu/userdata/Author_images/".$profile_pic_db ;
}

Upvotes: 0

Views: 157

Answers (2)

Neinrappeur Zaki
Neinrappeur Zaki

Reputation: 397

You will always get $pro_num !=0 , you will need to check the default value if you image column , lets say the default value is NULL ,

     $check_pic = mysqli_query($conn,"SELECT image FROM users WHERE id='$id'");
        $get_pic_row = mysqli_fetch_assoc($check_pic);
        $profile_pic_db = $get_pic_row['image'];
        $pro_num = mysqli_num_rows($profile_pic_db);
    if($pro_num == 1){

    if ($profile_pic_db == null) {
            $profile_pic = "http://localhost/Ramdhenu/images/default_propic.png";
        } else {
            $profile_pic = "http://localhost/Ramdhenu/userdata/Author_images/".$profile_pic_db ;
        }
    }else{
//user not found
}

Upvotes: 0

Qirel
Qirel

Reputation: 26460

You need to be checking the value of the column, not if any rows were returned. You should also be using a prepared statement instead.

$stmt = $conn->prepare("SELECT image FROM users WHERE id=?");
$stmt->bind_param("s", $id);
$stmt->execute();
$stmt->bind_result($image);
if ($stmt->fetch()) {
    if (empty($image)) {
        $profile_pic = "/Ramdhenu/images/default_propic.png";
    } else {
        $profile_pic = "/Ramdhenu/userdata/Author_images/".$image; 
    }
} else {
    // No user by that ID
}
$stmt->close();

Upvotes: 3

Related Questions