0

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.

huijing
  • 11
  • 2

2 Answers2

0

Here is an Example script you can use:

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

$query = "SELECT * FROM handover ORDER BY ID";

$stmt = $con->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
while($obj = $result->fetch_object()) {
echo($obj->ID.":   <img src='".$obj->image."' alt='IMG-".$obj->ID."' height='150' width='150'><br>");
echo($obj->description);
}
?>

Bind this code in to your form2.php or media.php Should be work. If you want some thing else in this code feel free to edit it.

NvrKill
  • 327
  • 2
  • 16
0

Ok, so it looks like you are confusing how images work.

Firstly, in the method you provided you aren't actually storing the image from the request. When you upload an image to the server it will not be in the $_GET global, because an image upload is different to a $_GET parameter. A GET parameter is a string parameter in the URL, e.g. google.co.uk?example_get=get

Secondly, you don't really want to store the image as a BLOB in the database, instead you want to store the image in the file system. Once you have stored the image you should save a relative file path to the image in the database.

Lastly, to return the image to a user you should fetch the file path and parse it into a URL, which you then return and place into the src attribute of an img tag.

For a more in depth tutorial with examples you should follow the premise of the link below:

https://makitweb.com/upload-and-store-an-image-in-the-database-with-php/

bgld
  • 31
  • 5