Jomai Jomai - 1 year ago 123
MySQL Question

Error uploading image to MySQL database

I'm planning to upload an image file to my MySQL database. Can someone please help me get this right? I tried so many methods but I still can't get to upload the image. I've been trying the code below, but it always gets an error like this:

Error: INSERT INTO uploaded_images VALUES ('', asdasdasd , C:\xampp\tmp\phpE7CE.tmp) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':\xampp\tmp\phpE7CE.tmp)' at line 1

<!DOCTYPE html>


if ($_POST) {

$imagename = $_POST['imagename'];
$imagedata = mysqli_real_escape_string($conn ,$_FILES['image']['tmp_name']);
$imagetype = $_FILES['image']['type'];

if(substr($imagetype,0,5) == "image") {

$sql = "INSERT INTO uploaded_images VALUES ('', $imagename , $imagedata)";

if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);


<form action="upload.php" method="POST" enctype="multipart/form-data">
Name: <input type="text" name="imagename"> <br>Select image to upload:
<input type="file" name="image" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">


Answer Source

I'm not sure if what you really want is to save images in a DB instead of an actual folder in your project, but anyway, the insert query should be formatted like this:

$sql = "INSERT INTO uploaded_images (image_name, image_data) VALUES ('$imagename' , '$imagedata')"; // assuming an autoincrement here

Your query must be failing; check your PHP error log.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download