上传图像时出现MySQL语法错误

时间:2013-04-07 20:22:42

标签: 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ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄ' 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>";
    }
}

1 个答案:

答案 0 :(得分:2)

显然图像文件内容中有撇号。这并不奇怪。您需要正确地转义输入(以及所有输入)。

$image = mysql_real_escape_string($_FILES['image']['tmp_name']);

您应该使用mysqli或PDO正确参数化查询,而不是使用ext/mysql。然后你不必明确逃避。

相关问题