将JPEG上传到MySQL时出错

时间:2011-05-09 19:34:16

标签: php mysql blob jpeg

尝试将JPEG图像上传到我的MySQL数据库时,我收到以下错误(图片是BLOB):

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 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 'WHERE id=57 (Image)  VALUES ('ÿØÿà\0JFIF\0\0\0\0\0\0ÿá\0XExif\0\0MM\0*\0\0\' at line 1

如果你能在我的代码中告诉我这个问题,我真的很感激。

$sql = sprintf(
"INSERT INTO recipies WHERE id=$id (Image) VALUES ('%s')", mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"])));
$results = mysql_query($sql) or die(mysql_error());

3 个答案:

答案 0 :(得分:1)

也许这样会更正确的语法

$sql = sprintf(
"INSERT INTO recipies (Image) VALUES ('%s') ", mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"])));
$results = mysql_query($sql) or die(mysql_error());

修改

似乎你对SQL UPDATE语法和MySQL特殊混乱感到困惑。所以正确的语法是

INSERT
    [INTO] tbl_name [(col_name,...)]
    {VALUES | VALUE} ({expr | DEFAULT},...),(...),...

INSERT 
    [INTO] tbl_name
    SET col_name={expr | DEFAULT}, ...

所以你的朋友是MySQL::INSERT Syntax Manual。 快乐的查询!

答案 1 :(得分:1)

插入或更新位置。

你可能想要这个:

$sql = sprintf(
"UPDATE recipies SET Image = '%s' WHERE id=$id", mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"])));
$results = mysql_query($sql) or die(mysql_error());

答案 2 :(得分:0)

看起来更像是在尝试更新而不是插入

UPDATE recipes SET Image= ('%s') WHERE id = %d