我打算将图片文件上传到我的MySQL数据库。有人可以帮我解决这个问题吗?我尝试了很多方法,但我仍然无法上传图像。我一直在尝试下面的代码,但总是会出现这样的错误:
错误:INSERT INTO uploaded_images VALUES('',asdasdasd,C:\ xampp \ tmp \ phpE7CE.tmp)SQL语法中有错误;检查与您的MariaDB服务器版本对应的手册,以便在第1行':\ xampp \ tmp \ phpE7CE.tmp)附近使用正确的语法
<!DOCTYPE html>
<html>
<body>
<?php
include("database/db_conection.php");
if ($_POST) {
$imagename = $_POST['imagename'];
$imagedata = mysqli_real_escape_string($conn ,$_FILES['image']['tmp_name']);
$imagetype = $_FILES['image']['type'];
if(substr($imagetype,0,5) == "image") {
$sql = "INSERT INTO uploaded_images VALUES ('', $imagename , $imagedata)";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
}
?>
<form action="upload.php" method="POST" enctype="multipart/form-data">
Name: <input type="text" name="imagename"> <br>Select image to upload:
<input type="file" name="image" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
答案 0 :(得分:1)
我不确定您真正想要的是将数据保存在数据库而不是项目中的实际文件夹中,但无论如何,插入查询的格式应如下所示:
$sql = "INSERT INTO uploaded_images (image_name, image_data) VALUES ('$imagename' , '$imagedata')"; // assuming an autoincrement here
您的查询必定失败;检查你的PHP错误日志。