Jump to content

How to send images into MySQL database?


moby04

Recommended Posts

Hi. I've prepared a database containing one table:

 

create table images (
id int auto_increment primary key,
mime char(15),
image mediumblob not null);

 

Later on I made an html form to send an image and a PHP script to receive and check it. I have already checked if receiving is correct by sending the given file as an image to the browser and it was correct. After receiving the script should send the image to the database and close connection. Unfortunately something goes wrong when I am sending a query. I use the following code:

 

@ $db = new mysqli($db_host, $db_user, $db_pass, $db_name);
if(mysqli_connect_errno())
    echo "Connection failed..<br />\n";
$content = file_get_contents($_FILES['sentfile']['tmp_name']);
$query = 'insert into images (mime, image) values ("'.$_FILES['sentfile']['type'].'", "'.$content.'")';
echo $query;
$outcomes = $db->query($query);
if($outcomes)
    echo $db->affected_rows;
else
    echo $db->error;
$db->close();

 

And it throws 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 '' at line 1
. I have also tried with some basic string instead of jpeg image and then it works fine. What should I do?
Link to comment
Share on other sites

the better thing to do would be to merely save a reference of the image to the database, and the actual image is stored in a directory somewhere on the server.  i think you are either using the wrong datatype to store the image or your image is failing to upload, causing a blank string (which mysql doesnt like for some reason).

 

also, use sprintf when writing queries, it makes things a lot easier.

Link to comment
Share on other sites

I know that usually the better idea is to store only reference but I need to do it this way in this specific situation. Then, i used a mediumblob which can store up to 16MB of binary safe data so it should be ok... and about the upload, is it possible that it fails at sending data between PHP and MySQL?

 

And one more thing... Error message claims the error is next to '' sign while in fact I don't use such mark in my query. Can I use addslashes() with binary data to solve it?

Link to comment
Share on other sites

I have also tried with some basic string instead of jpeg image and then it works fine. What should I do?

that is a pretty important detail.  do you think that your query maybe truncated?  try a very small, 1 by 1 jpeg image and see if it completes.  if not, there is something wrong with your file uploading/retrieval section of the code.

 

if your query looks fine, though, after echoing, then i am afraid the error is beyond me...

 

(i also know that images and any binary data for that matter can contain quotes in it as a result of encoding data... make sure this isnt the case either)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.