1

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>";
    }
}
4

1 に答える 1

2

どうやら、画像ファイルの内容にアポストロフィが含まれているようです。それはそれほど驚くべきことではありません。入力 (およびそのすべての入力) を適切にエスケープする必要があります。

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

を使用する代わりにext/mysql、mysqli または PDO で適切にパラメーター化されたクエリを使用する必要があります。その後、明示的にエスケープする必要はありません。

于 2013-04-07T20:25:09.927 に答える