Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am having a hard time figuring out how to display photos from the database using php. So, far I can successfully add text and photos into the database, in addition to displaying the text, however I am just not able to display my photos from the database. I have a feeling that it has to do with the fact that I am not able to add any of the uploaded photos into my images folder or the path to this folder is not correct. Please see my code below, and it would be great if I could get some help:

Here is my uploadtest.php file:

<?php

if(isset($_POST['upload'])){//if upload button is pressed


//the path to store uploaded images
$target = "images/" .basename($_FILES['image']['name']);


//connect to database
$conn = mysqli_connect('127.0.0.1', 'root', '', 'photos');
// get all the submitted data from the form
$image=$_FILES['image']['name'];
$text =$_POST['text'];

$sql ="INSERT INTO images (image, text) VALUES ('$image', '$text')";
mysqli_query($conn, $sql); //stores data into database table images

//lets move uploaded image into folder
if(move_uploaded_file($_FILES['image']['tmp_name']. $target, "images/")){
$msg="Image uploaded successfully.";
}else{
$msg="There was a problem uploading the image";
}


}

?>

<!DOCTYPE html>


<title>Image upload

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">



<?php
$conn = mysqli_connect('127.0.0.1', 'root', '', 'photos');
$sql="SELECT * FROM images";
$result = mysqli_query($conn,$sql);

while($row = mysqli_fetch_array($result)){
echo "
";
echo "<img src='images/'".$row['image']."'>";
echo "

".$row['text']."

";
echo "
";

}
?>















And here is my css file:

#content{
width:50%;
margin:20px auto;
border:1px solid #cbcbcb;
}
form{
width:50%;
margin:20px auto;
}
form div{
margin-top:5px;
}
#img_div{
width:80%;
padding:5px;
margin:15px auto;
border:1px solid #cbcbcb;
}
#img_div:after{
content:"";
display:block;
clear:both;
}
img{
float:left;
margin:5px;
width:300px;
height:140px
}

Thanks again!

What I have tried:

I have been watching many youtube videos but am still struggling with this problem.
Posted
Updated 2-Aug-17 19:37pm

1 solution

To get image from database you have to try like this.
<?php

  $id = $_GET['id'];
  // do some validation here to ensure id is safe

  $link = mysql_connect("localhost", "root", "");
  mysql_select_db("dvddb");
  $sql = "SELECT dvdimage FROM dvd WHERE id=$id";
  $result = mysql_query("$sql");
  $row = mysql_fetch_assoc($result);
  mysql_close($link);

  header("Content-type: image/jpeg");
  echo $row['dvdimage'];
?>
 
Share this answer
 
Comments
Member 13342809 3-Aug-17 20:59pm    
Thank you for the information, however I am now getting this error message:

The image "http://localhost/Email/uploadtest.php" can't be displayed because it contains errors.
Mohibur Rashid 4-Aug-17 6:02am    
Change content type to text and read the error
Member 13342809 4-Aug-17 9:55am    
Thank you for the response. Here are the error messages that I am getting:


Notice: Undefined index: id in C:\xampp\htdocs\Email\images\uploadtest.php on line 68

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Email\images\uploadtest.php on line 75

Notice: Undefined variable: link in C:\xampp\htdocs\Email\images\uploadtest.php on line 76

Warning: mysql_close() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Email\images\uploadtest.php on line 76

This is my code so far:

<?php

if(isset($_POST['upload'])){//if upload button is pressed

//$ext_type = array('gif','jpg','jpe','jpeg','png');
//the path to store uploaded images
$target = "images/" .basename($_FILES['image']['name']);

//$ext_type = array('gif','jpg','jpe','jpeg','png');//checks to see if file is one of these

//connect to database
$conn = mysqli_connect('127.0.0.1', 'root', '', 'photos');
//get all the submitted data from the form
$image=$_FILES['image']['name'];
$text =$_POST['text'];

$sql ="INSERT INTO images (image, text) VALUES ('$image', '$text')";
mysqli_query($conn, $sql); //stores data into database table images

//lets move uploaded image into folder
if(move_uploaded_file($_FILES['image']['tmp_name']. $target, "images/")){
$msg="Image uploaded successfully.";
}else{
$msg="There was a problem uploading the image";
}

//$sql="SELECT * FROM images";
//$result = mysqli_query($conn, $sql);
//while($row= mysqli_fetch_array($result)){
//echo "<img src = 'Image/".$row['image'].'" />';
//echo ' ';
// $res= mysql("SELECT * FROM images");
// $res = mysqli_query($conn, sql);
//echo "";
// echo "";

//while($row = mysqli_fetch_array($res)){
// echo "";
// echo '< height='100' width='100'/>;

}

//if(isset($_POST['sumbit2'])){
// $res = mysql_query("SELECT * FROM image");
//echo "";
//echo "";
//while($row = mysql_fetch_array($res)){
//echo "";
//echo '<img scr = data:image/jpeg; base_64'.base64_decode($row['image']).'" height="100" width="100"/>';
//}
//echo "";
//}
?>

<!DOCTYPE html>


<title>Image upload

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">



<?php
$conn = mysqli_connect('127.0.0.1', 'root', '', 'photos');

$id = $_GET['id'];
// do some validation here to ensure id is safe

// $link = mysql_connect("localhost", "root", "");
// mysql_select_db("photos");
$sql = "SELECT image FROM images WHERE id=$id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);

header("text: image/jpeg");
echo $row['image'];
/*
$sql="SELECT * FROM images";
$result = mysqli_query($conn,$sql);

while($row = mysqli_fetch_array($result)){
echo "";
echo "<img src='images/'".$row['image']."'>";
echo "".$row['text']."";
echo "";

}
?>*/
?>









This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900