如何从数据库中检索和显示图像?

时间:2019-07-17 13:08:38

标签: php html

我有一个表格,需要附加图像文件并将其上传到数据库,并且能够将其插入数据库。但是,我无法从数据库中检索图像以显示它。如何从数据库获取图像并显示?

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';
}

?>

我希望该图像能够从表单上载的数据库中检索出来,并在网页中显示该图像。

2 个答案:

答案 0 :(得分:0)

以下是您可以使用的示例脚本:

<?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);
}
?>

将此代码绑定到您的form2.phpmedia.php中应该可以使用。如果您想要这段代码中的其他内容,请随时对其进行编辑。

答案 1 :(得分:0)

好吧,看来您对图像的工作方式感到困惑。

首先,在您提供的方法中,实际上并没有存储请求中的图像。当您将图像上载到服务器时,它将不在$ _GET全局中,因为图像上载不同于$ _GET参数。 GET参数是网址中的字符串参数,例如google.co.uk?example_get=获取

第二,您实际上并不希望将图像作为BLOB存储在数据库中,而是希望将图像存储在文件系统中。存储图像后,应在数据库中保存该图像的相对文件路径。

最后,要将图像返回给用户,您应该获取文件路径并将其解析为URL,然后将其返回并放入img标签的src属性。

有关示例的更深入教程,请遵循以下链接的前提:

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

相关问题