錯誤:嘗試將圖像上載到數據庫,然後顯示數據庫。得到一個致命錯誤
Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\FinalProject\upload-profilePic.php:22 Stack trace: #0 {main} thrown in C:...
我的代碼:
上傳圖片
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (file_exists($_FILES['upload_file']['tmp_name'])) {
// get temp name and file name
$tmpName = $_FILES['upload_file']['tmp_name'];
$fileName = $_FILES['upload_file']['name'];
$type = $_FILES['upload_file']['type'];
$handler = fopen($tmpName, 'r'); // opens image file stream
$data = fread($handler, filesize($tmpName)); // read byte data
fclose($handler); // close image file stream
$stmt = $con->prepare('insert into images (image_type,
filename, image_data) values(?, ?, ?)');
$stmt->bind_param('sss', $type, $fileName, $data);
$stmt->execute();
$result = $stmt->get_result();
if (!$result) {
echo $con->error;
}
}
}
$result = $con->query('select * from images');
if ($result) {
foreach ($result as $img) {
$name = $img['filename'];
$id = $img['image_id'];
$url = "get-profilePic.php?id=$id";
echo "<h3>$name</h3>";
echo "<img src=\"$url\" alt=\"$name\" />\n\n";
}
}
?>
獲取圖像
<?php
$imageId = $_GET['id'];
$result = $con->query("select * from images
where image_id = $imageId");
if ($result) {
$image = $result->fetch_object();
$type = $image->image_type;
$data = $image->image_data;
header('Content-Type: ' . $type);
echo $data;
}
?>
什麼是$ con?以及它是如何創建的? –
您可以嘗試不將圖像直接上傳到您的數據庫。您只需將鏈接作爲參考插入數據庫即可。然後,您可以稍後調用該鏈接來獲取圖像並將其用於您網站的任何位置。希望這是有道理的。 –
@IanKemp $ con是我與數據庫的連接。它被定義。我更改了一個列名,但沒有更新它。感謝您的幫助。 – ferrari3000