繁体   English   中英

如何使用mysqli prepare从数据库中检索图像?

[英]how to retrieve image from database using mysqli prepare?

我正在尝试将我的代码转换为mysqli准备,并且在从数据库中检索特定数据的图像时遇到问题。 例如,假设id 1将其图像发布在同一页面中。

您能检查一下我的密码吗? 我正在尝试查看图像的特定数据。 你能帮我吗? 谢谢,这是我的代码:

这是我形式的代码:

<form action="upload_photo.php" method="POST" enctype="multipart/form-data">
    <div class="col-md-6">
        <div class="alert alert-info" role="alert"><span class="glyphicon glyphicon-user" aria-hidden="true"></span>&nbsp;&nbsp;Add/Update Photo</div>
        <input type="hidden" name="id" value="<?= $id; ?>" />
        <input type="file" name="image"><br>
        <input type="submit" value="Upload Image" name="submit">
    </div>
    <div class="row">
        <div class="col-md-6 col-md-3">
            <a href="#" class="thumbnail">
            <img src="image_view.php?id=$id" alt="...">
            </a>
        </div>
            Updated Photo 
    </div>
</form>

这是我的php上传图片的代码:

<?php

include '../session.php';
require_once 'config.php';


if (isset($_POST['submit'])) {

    $imageName = mysqli_real_escape_string($conn, $_FILES["image"]["name"]);
    $imageData = mysqli_real_escape_string($conn, file_get_contents($_FILES["image"]["tmp_name"]));
    $imageType = mysqli_real_escape_string($conn, $_FILES["image"]["type"]);

        if (substr($imageType, 0,5) == "image") {
            $query = "UPDATE `crew_info` SET `image_name` = ?, `updated_photo` = ? WHERE `id` = ?";
            $stmt = mysqli_prepare($conn, $query);
            mysqli_stmt_bind_param($stmt, 'ssi', $imageName, $imageData, $_POST['id']);
            mysqli_stmt_execute($stmt);
            $id = $_POST['id'];

            header("Location: ../admin/view_all_info.php?id=$id");

        }

        else {

            echo "Image not Uploaded!";

        }

}

?>

这是我检索图像的代码,但不适用于我:

<?php

include '../session.php';
require_once 'config.php';

if (isset($_POST['id'])) {

    $id = mysqli_real_escape_string($_POST['id']);
    $query = "SELECT * FROM `crew_info` WHERE `id` = ?";
    $stmt = mysqli_prepare($conn, $query);
    mysqli_stmt_bind_param($stmt, 's', $_POST['id']);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_bind_result($stmt, $id, $first_name, $middle_name, $last_name, $age, $month, $day, $year, $birth_place, $gender, $martial_status, $religion, $nationality, $email_address, $address_1, $address_2, $course, $school_graduated, $remarks, $date_added, $crew_status, $image_name, $updated_photo);

    while (mysqli_stmt_fetch($stmt)) {

        sprintf("%s", $updated_photo);
    }

    header("content-type: image/jpeg");
    sprintf("%s", $updated_photo);

}
else {
    echo "Bad";
}



?>

图片正在上传,但无法检索。 请帮我,谢谢

这里有一些问题:

  1. 在您的html中,使用<img src="image_view.php?id=$id" alt="..."> 这会向服务器发出GET请求,因此您在输出图像的脚本中需要$_GET['id']而不是$_POST['id']
  2. 使用预备语句时,不应使用mysqli_real_escape_string() ,当执行预备语句时,mysqli会处理。
    另外,在用于获取图像的脚本中,您没有将数据库连接作为第一个参数发送,因此也会使它失败。 编辑:您似乎并没有使用转义的值,因此您可以简单地删除该行。

除此之外,您可能还应该:

  • 将图像作为文件存储在文件系统中。 如果内容不敏感/受保护,则可以将其放在Web服务器的根目录中,以便可以轻松地包含指向它的链接。 这将使数据库的维护和备份更加容易。
  • 如果使用现在使用的方法,则仅在显示图像时得到所需的内容。 这将使您的代码更易于阅读,并且只要您这样做,新的列就不会引入错误:
    SELECT updated_photo FROM crew_info WHERE id = ?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM