Question:
PHP/MYSQL insert image help...?
Ranger Giya
2009-10-22 08:23:59 UTC
So I am trying to insert an image to my database...

I have this table in my database...this part work...
CREATE TABLE IF NOT EXISTS spacelocation (
spacelocationid BIGINT NOT NULL AUTO_INCREMENT, PRIMARY KEY(spacelocationid),
image BLOB,
name VARCHAR(255) NOT NULL UNIQUE,
);
the spaceloctionid and name already exist...i just need to add the image...

and this is the php...
// Read the image bytes into the $data variable
$fh = fopen("planet1.JPG", "rb");
$data = addslashes(fread($fh,filesize("planet1.JPG")));
fclose($fh);

// Create the query
$SQL = "INSERT INTO spacelocation(image) VALUES(\"".$data."\) WHERE spacelocationid = 2";
// Execute the query
$RESULT = mysql_query($SQL) or die("Couldn't insert image");

but I keep getting the error and it just says Couldn't insert image.

what am I doing wrong please help...
Three answers:
just "JR"
2009-10-22 09:45:28 UTC
A few problems:

1. You do NOT insert an image in a database! You store the image in a given folder, and store the PATH only in the database. This will make the search and retrieval faster and will prevent overloading your DB.

2. Your insert is incorrect:

OR

$sql = "insert into `spacelocation` (`image`) values ('".$data."')";

expanded with spaces: values ( ' " . $data . " ' ) " ; Note ` ' and " !

OR

$sql = "update `spacelocation` set `image`='".$data."' where `spacelocationid`='2'";

expanded again: `image` = ' " . $data . " ' and ...tionid` = ' 2 ' " ;

BUT :

$sql = "update `spacelocation` set `image`='".$filepath."' where `..id`='2' limit 1";

would be better, and your image field can be set to VARCHAR (25) (or more).

NOTE:

Fields of type LONGTEXT, BLOB etc do NOT increase the size of the database: the field stores a pointer to an "invisible" file that contains the data...

3. Your query itself would benefit from re-write:

mysql_query($sql) or die ("Error: ".mysql_error());

OR

mysql_query($sql);

if (mysql_error())

return ("Error in function abc: ".mysql_error());

which has the advantage of allowing you to control the error. "die" is fine, but it is the end of the story, and you have no way of controlling the error!
jacer17
2009-10-22 16:06:44 UTC
First thing you can do is. change die("Couldn't insert image"); to

die("Couldn't insert image
" . mysql_error());. That will give you an error with short description.



You're Insert sql line seems odd. You have a WHERE in there. You don't use WHERE. If you want to insert into your database your data, where spacelocationid = 2, then you create the row where the id = 2. For example:



VALUES ('2',"\"" . $data . "\"")



If you have a row where the id = 2, then you use MySql Update.
Namibnat
2009-10-22 19:17:53 UTC
I second the answer that just "JR" gave,



Don't put an image in your database, store it's url! Put the picture in a sub folder of the public_html part of your server (or where you have put your normal php pages.) Only store a reference to it in the database.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...