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.