Php 上载图像时出现MySQL语法错误
我在将BLOB上载到MySQL数据库时遇到问题,出现以下错误:Php 上载图像时出现MySQL语法错误,php,mysql,image,file-upload,syntax,Php,Mysql,Image,File Upload,Syntax,我在将BLOB上载到MySQL数据库时遇到问题,出现以下错误: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄ' a
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄ' at line 1
我知道这个错误会导致图像的文件内容,但我不知道语法有什么问题。有什么建议吗?谢谢
以下是PHP:
$file = $_FILES['image']['tmp_name'];
// If there's no file selected when button is pressed, echo out and tell the user to select an image to upload
if (!isset($file))
echo "<p>Please select an image to upload.</p>";
else {
//mysql escape string
$image = file_get_contents($_FILES['image']['tmp_name']);
//and here
$image_name = $_FILES['image']['name'];
$imagesize = getimagesize($_FILES['image']['tmp_name']);
}
// Checks that the file being uploaded is an image, i.e. has a size attribute with height & width dimensions
if ($imagesize == FALSE)
echo "<p>Please upload only an image file such as .jpg or .png.</p>";
else {
$sql = "INSERT INTO design (id, caption, image) VALUES ('', '$image_name', '$image')";
$result = mysql_query($sql);
if (!$result)
echo "<p>Something went wrong.</p>" . mysql_error();
else {
echo "<p>Thank you for submitting your design.</p>";
}
}
$file=$\u文件['image']['tmp\u name'];
//如果按下按钮时未选择任何文件,则回显并告诉用户选择要上载的图像
如果(!isset($file))
echo“请选择要上载的图像。”;
否则{
//mysql转义字符串
$image=file_get_contents($_FILES['image']['tmp_name']);
//这里呢
$image\u name=$\u文件['image']['name'];
$imagesize=getimagesize($_文件['image']['tmp_名称]]);
}
//检查正在上载的文件是否为图像,即具有具有高度和宽度尺寸的大小属性
如果($imagesize==FALSE)
echo“请仅上载.jpg或.png等图像文件。”;
否则{
$sql=“插入设计(id、标题、图像)值(“'$image_name'、'$image')”;
$result=mysql\u查询($sql);
如果(!$result)
echo“出现问题。”.mysql\u error();
否则{
echo“感谢您提交设计。”;
}
}
显然,图像文件内容中有一个撇号。这并不奇怪。您需要正确地转义输入(以及与此相关的所有输入)
不要使用
ext/mysql
,而应该使用mysqli或PDO的正确参数化查询。那么你就不必显式地转义。你必须将图像存储在文件系统中,而不是在数据库中填充图像。你的答案唯一的问题是,你仍然在使用mysql_*函数,这些函数已经贬值,我建议你不要使用它们。@Diemuzi我在第二段中这样做了谢谢!我甚至在代码中添加了注释,将mysql\u real\u escape\u string
添加到这两个变量中!我是个白痴。
$image = mysql_real_escape_string($_FILES['image']['tmp_name']);