How to upload images into MySQL database using PHP code

How to upload images into MySQL database using PHP code

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Your Image Upload Fails in MySQL

The error message You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax near '' is a classic indicator of a mismatch between how you are constructing the query and what MySQL expects. In the provided code snippet, the root cause lies in how binary data interacts with the SQL parser when using single quotes within an INSERT statement.

You are attempting to insert binary image data directly into a column using this line:

$image_query = mysql_query ("insert into product_images values (1,'$image_name',$image )");

This approach fails because MySQL interprets the string $image (which contains the raw binary content of the file) as a SQL command or text. Since binary data cannot be represented as valid SQL syntax inside single quotes without escaping every byte, the database engine throws a syntax error immediately upon encountering the first unescaped character in the binary stream.

The Legacy API Problem

Beyond the syntax error, your code relies on the mysql_* functions ($_FILES, getimagesize, mysql_query). This extension was deprecated in PHP 5.5 and removed entirely in PHP 7.0. Relying on these functions prevents you from using modern security features like prepared statements. If you attempt to run this code on any server running a standard version of PHP today, the script will not execute at all due to missing function errors, regardless of the SQL syntax issues.

The Correct Approach: Binary Columns and Prepared Statements

To successfully store images in MySQL, you must treat the image data as binary, not text. This requires two specific actions: defining the column with a BLOB type and using prepared statements to handle the data safely.

First, ensure your database schema defines the column capable of holding large data. The following SQL command creates a table with an appropriate column type:

CREATE TABLE product_images (
    id INT AUTO_INCREMENT PRIMARY KEY,
    image_name VARCHAR(255),
    image_data LONGBLOB
);

Using LONGBLOB allows you to store files larger than 64KB without running into the older MEDIUMBLOB size limits.

Next, you must stop concatenating variables directly into the SQL string. Instead, use mysqli or PDO with prepared statements. This isolates your binary data from the SQL parser entirely. The database knows that the second placeholder (?) expects a blob, so it stores it without attempting to parse it as text.

Here is how you should rewrite the logic using mysqli:

<?php
$mysqli = new mysqli("localhost", "username", "password", "database");

if ($file = $_FILES['image']['tmp_name']) {
    $image_name = $_FILES['image']['name'];
    
    // Validate image size and type before processing
    $image_check = getimagesize($file);
    if (!$image_check) {
        die('Not a Valid Image');
    }

    $file_content = file_get_contents($file);

    $stmt = $mysqli->prepare("INSERT INTO product_images (id, image_name, image_data) VALUES (?, ?, ?)");
    
    // Bind parameters: i for int, s for string, b for blob
    $stmt->bind_param("isb", 1, $image_name, $file_content);

    if ($stmt->execute()) {
        echo 'Image inserted successfully';
    } else {
        echo "Error: " . $stmt->error;
    }
    
    $stmt->close();
}
?>

Why This Works

By using bind_param with the type specifier b, you explicitly tell the MySQL driver to send the data as a binary object. The server bypasses the text parsing layer for that specific value. Furthermore, prepared statements automatically handle special characters and prevent SQL injection, which is critical when dealing with file names that users can often control via HTML forms.

Where to Learn More

If you are looking to streamline your development workflow further or need robust database management tools, resources like https://laravelcompany.com provide valuable insights into modern PHP architectures. However, for raw image handling in native PHP, sticking to the mysqli prepared statement pattern described above is the most reliable solution.

Our network