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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.