我正在尝试将我的代码转换为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";
}
?>
图片正在上传,但我无法检索。请帮帮我们谢谢你们
答案 0 :(得分:0)
这里有一些问题:
<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 = ?
。