1

I'm having issue uploading a BLOB into my MySQL database and get the following error:

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

I know the error is resulting in the image's file contents but I can't figure out what's wrong with the syntax. Any suggestions? Thanks!

Here's the 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
  • Instead of stuffing images in database you have to store them in a filesystem Commented Apr 7, 2013 at 20:27

1 Answer 1

2

Apparently the image file contents has an apostrophe in it. That's not that surprising. You need to properly escape the input (and all inputs for that matter).

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

Instead of using ext/mysql, you should use properly parameterized queries with mysqli or PDO. Then you don't have to escape explicitly.

Sign up to request clarification or add additional context in comments.

3 Comments

Only problem with your answer is that you're still using the mysql_* functions which have been depreciated and I would recommend not using them.
@Diemuzi I do in the second paragraph
Thanks! I even commented in my code to add mysql_real_escape_string to both those variables! I'm an idiot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.