0

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 
}   

?>
3
  • what is the value stored for image in the db? also, the path in the img tag has a leading space before ` images/` Commented May 6, 2017 at 12:16
  • this is the value that is saved in image column "images/download.jpg" Commented May 6, 2017 at 12:27
  • What do you mean by "does not work"? What happens instead? Also, be warned that your INSERT query is highly vulnerable for SQL injection - have a look at prepared statements to avoid getting hacked Commented Mar 1, 2024 at 15:58

3 Answers 3

2

remove images from image path, you already store this in image column in images table

echo "<img src='/".$row['image']."'>"; // the problem is here, its just displaying img icon, not actual image 
Sign up to request clarification or add additional context in comments.

1 Comment

echo "<img src='/".$row['image']."'>"; // your code had '/' below line is correct echo "<img src='".$row['image']."'>"; Thanks
0

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.

Comments

0

You are adding images/ into the database then when you call the row to display the image you're adding images/ adding making it look in images/images/filename

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.