[英]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> 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";
}
?>
图片正在上传,但无法检索。 请帮我,谢谢
这里有一些问题:
<img src="image_view.php?id=$id" alt="...">
。 这会向服务器发出GET请求,因此您在输出图像的脚本中需要$_GET['id']
而不是$_POST['id']
。 mysqli_real_escape_string()
,当执行预备语句时,mysqli会处理。 除此之外,您可能还应该:
SELECT updated_photo FROM crew_info WHERE id = ?
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.