954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Images are not storing in MYSQL database.

Hi,

I am having an issue saving images to a MYSQL database as a BLOB, I have been for a few days now, I have used several different tutorials and even though the code works with no errors returned, nothing actually get put in my database.

Here is my form to upload to my database, this is on a file called photo.php.

<form enctype="multipart/form-data" action="insert.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" id="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>


Its very simple as its all I need for now, as you can see the action is taken from insert.php so here is the php code from insert.php which should save the image to my database.

<?php
include_once 'tables.php';

// Make sure the user actually
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

      // Temporary file name stored on the server
      $tmpName  = $_FILES['image']['tmp_name'];

      // Read the file
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);


      // insert into our database.
      $result = mysql_query("INSERT INTO tbl_images ('image') VALUES ('$data')");

      // Print results
      print "Thank you, your file has been uploaded.";
      
}

?>


here is the code for my table which is also very simple for now

CREATE TABLE tbl_images (
id tinyint(3) unsigned NOT NULL auto_increment,
image blob NOT NULL,
PRIMARY KEY (id)
);


This code includes tables.php as it has the access details for the database.

Was hoping someone could help me find the issue to why my images won't save to my database.

Thanks.

AdriftUniform
Light Poster
39 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

I have managed to fix this, I realised that I was not submitting a NULL value into the database for field 'id'.

Sorry for any inconvenience.

AdriftUniform
Light Poster
39 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: