猿问

php 检查用户是否上传了个人资料图片

你好如何检查用户是否上传了个人资料图片...我试过这个代码但只显示默认图片而不是其他部分...请帮助...这是我的代码


$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 ;

}


catspeake
浏览 131回答 2
2回答

隔江千里

您需要检查列的值,而不是是否返回任何行。您还应该改用准备好的语句。$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();

蓝山帝景

您将始终获得 $pro_num !=0 ,如果您对列进行图像处理,则需要检查默认值,假设默认值为 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}
随时随地看视频慕课网APP
我要回答