简体   繁体   中英

How to display images from a database using PHP?

My program needs to upload images, so an image (varchar) location is saved in a MySQL database. It's working so far.

Now I want to display images and this does not work. Here is the code:

include ('connect.php');
if(isset($_POST['submit'])){
    $filetemp= $_FILES['image'] ['tmp_name'];
    $filename= $_FILES['image'] ['name'];
    $filepath= "images/".$filename;

    move_uploaded_file($filetemp,$filepath);
    $sql=mysqli_query($con,"insert into  images (image) value ('$filepath')");
    if($sql){
        echo "uploaded";
    }
    else{
        echo " not uploaded";
    }
}

$sql=mysqli_query($con,"select * from images");
while($row=mysqli_fetch_array($sql)){
    echo "<img src=' images/".$row['image']."'>"; // the problem is here, its just displaying img icon, not actual image 
}   

?>

从图像路径中删除images ,您已经将其存储在images表的image列中

echo "<img src='/".$row['image']."'>"; // the problem is here, its just displaying img icon, not actual image 

Correction :

$sql=mysqli_query($con,"select * from images");
while($row=mysqli_fetch_array($sql)){
    echo "<img src='".$row['image']."'>"; // the problem is here, its just displaying img icon, not actual image 
}

Also you need to get the actual path via __FILE__ in case if needed.

您将图像/添加到数据库中,然后在调用该行以显示图像时添加图像/添加使其在图像/图像/文件名中显示

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM