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

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


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>

插入2.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")


?>


富国沪深
浏览 111回答 2
2回答

杨__羊羊

这是您可以使用的示例脚本:<?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.":&nbsp; &nbsp;<img src='".$obj->image."' alt='IMG-".$obj->ID."' height='150' width='150'><br>");echo($obj->description);}?>将此代码绑定到您的form2.php或media.php应该工作。如果您想要此代码中的其他内容,请随时对其进行编辑。
打开App,查看更多内容
随时随地看视频慕课网APP