无法使用php将图像上载到mysql数据库

无法使用php将图像上载到mysql数据库,php,image,upload,Php,Image,Upload,我正在尝试使用php5脚本将图像上传到MySQL数据库。我收到一个错误通知 错误,查询失败 UploadImage.php 图像上传 图像上传页面 从查询中的表字段中删除“”。使用此查询: $query = "INSERT INTO img_tbl (img_name, img_type, img_size, img_data ) VALUES ('$fileName', '$fileType', '$fileSize', '$imgC

我正在尝试使用php5脚本将图像上传到MySQL数据库。我收到一个错误通知

错误,查询失败

UploadImage.php


图像上传
图像上传页面

从查询中的表字段中删除“”。使用此查询:

$query = "INSERT INTO img_tbl (img_name, img_type, img_size, img_data )
                            VALUES ('$fileName', '$fileType', '$fileSize', '$imgContent')";
另外,请开始使用PDO或mysqli,因为您的查询是为sql注入打开的

$query = "
INSERT INTO `img_tbl` 
(`img_name`, `img_type`, `img_size`, `img_data` )
VALUES
('".$fileName."', '".$fileType."', '".$fileSize."', '".$imgContent."')
";

似乎
$imgContent
中的某些特殊字符正在破坏查询字符串

在发送到数据库之前,请使用
mysql\u real\u escape\u string
格式化数据

更新

如果第一个解决方案没有解决问题,请检查是否存在任何空值,您有一些数据库列设置为NOTNULL。因此不能向它们插入空值


希望这有帮助:)

请共享您的数据库结构尝试打印echo mysql\u errno($dbconn)。": " . mysql\u错误($dbconn)。“\n”;打印($文件)时会得到什么?是否有空值?我已在以下位置插入打印回显:mysql\u query($query)或die('Error,query failed'。mysql\u errno($dbconn)。“:”。mysql\u Error($dbconn)。“\n”);错误:错误,查询失败1064:您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,了解在“()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz”附近使用的正确语法�������������' 在第2行,$imgContent中的某些特殊字符正在破坏查询字符串,请检查我的答案:)您好,我仍然有相同的错误。。不过不管怎样,谢谢你的回答。嗨,我已经试过了,但还是一样的错误。。劝告谢谢你的帮助。
 $fileName   = mysql_real_escape_string($_FILES['imgfile']['name']); // image file name
 $tmpName    = $_FILES['imgfile']['tmp_name']; // name of the temporary stored file name
 $fileSize   = mysql_real_escape_string($_FILES['imgfile']['size']); // size of the uploaded file
 $fileType   = mysql_real_escape_string($_FILES['imgfile']['type']); //


 $fp   = fopen($tmpName, 'r'); // open a file handle of the temporary file
 $imgContent  = fread($fp, filesize($tmpName)); // read the temp file
 $imgContent  = mysql_real_escape_string($imgContent);
 fclose($fp); // close the file handle