I have a form where I required to attach an image file and upload to database and I am able to insert it to the database. However I am unable to retrieve the images from the database to display it. How do i get images from database and display?
form2.php:
<form action="insert2.php" method="GET" enctype="multipart/form-data">
<div class="container">
<div class="row">
<h2>3. Description of Item(s) </h2>
</div>
<div class="col-xs-12">
<div class="styled-input wide">
<textarea name="description" required /></textarea>
</div>
// this is the file attachment where it allows to select file from computer.
<div>
<label>Attachment:</label><input type='file' name='img' ><br>
</div>
</div>
</form>
insert2.php:
$con= mysqli_connect('127.0.0.1','root','');
if(!$con)
{
echo 'Not Connected To Server';
}
if(!mysqli_select_db($con,'satsform1'))
{
echo 'Database Not Selected';
}
$description = $_GET['description'];
$image = $_GET['img'];
//insert image to database.
$sql = "INSERT INTO handover (description,image)
VALUES ('$description','$image')";
if(!mysqli_query($con,$sql))
{
echo 'Not Submitted';
}
else
{
echo 'Submitted';
}
header("refresh:2; url=selection.php")
?>
fetch3.php:
<?php
$connect = mysqli_connect('127.0.0.1','root','', 'satsform1');
$output = '';
if(isset($_POST["query"]))
{
$search = mysqli_real_escape_string($connect, $_POST["query"]);
$query = "
SELECT * FROM handover
WHERE name LIKE '%".$search."%'
OR staffno LIKE '%".$search."%'
OR date LIKE '%".$search."%'
OR email LIKE '%".$search."%'
";
}
else
{
$query = "
SELECT * FROM handover ORDER BY ID
";
}
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>ID</th>
<th>Image</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["ID"].'</td>
<td><img src="'.$row["img"].'"></td>
</tr>
';
}
echo $output;
}
else
{
echo 'Data Not Found';
}
?>
I expect the image to be able to retrieve from the database where it is uploaded from the form and display the image in the web page.