Problem adding an image to database using SQL

339 views Asked by At
INSERT INTO items (id,name,image,price) VALUES('1','iphone 5s',LOAD_FILE('C:\xampp\htdocs\project\1.jpg'),300);

this statement cause error

INSERT INTO items (id,name,image,price) VALUES('1','iphone 5s',LOAD_FILE('C:\xampp\htdocs\project\1.jpg'),300)
MySQL said: Documentation

here is the error

#1048 - Column 'image' cannot be null
2

There are 2 answers

0
Rick James On

You must escape backslashes in any string:

... LOAD_FILE('C:\\xampp\\htdocs\\project\\1.jpg') ...

Or, since MySQL will 'correctly' interpret forward-slashes, even on Windows:

... LOAD_FILE('C:/xampp/htdocs/project/1.jpg') ...

(I assume image is declared BLOB or MEDIUMBLOB?)

0
Json On

Please double check the file path of your "1.jpg" or maybe look at the image format whether it is .jpg or .png.

Else, try this format:

LOAD_FILE('../1.jpg')

"Different backslash position"

I do not think you should mention the C:/xampp/htdocs/project because you are already in the project folder when you run your code.

Hopefully it is working.