Page 1 of 1

Storing image in a database - won't save

Posted: Wed Feb 04, 2009 9:12 am
by tenacious-dee
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I'm trying to use the code below to write an image into a long blob field in my mysql database.

Code: Select all

<?php
    // Create MySQL login values and 
    // set them to your login information.
    $username = "root";
    $password = "*****";
    $host = "localhost";
    $database = "anml";
    
    // Make the connect to MySQL or die
    // and display an error.
    $link = mysql_connect($host, $username, $password);
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    
    // Select thedatabase
    mysql_select_db ($database);
    
    // 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);
          
    
          // Create the query and insert
          // into the database.
          $query = "UPDATE tblhomepage SET image='$data' WHERE homepage_id=4";
          $results = mysql_query($query, $link);
          
          // Print results
          echo "<br/><br/><br/>";
          echo "<center>";
          echo "<p>Thank you, your file has been uploaded.</p>";
          echo "</center>";
 
    }
    else {
          echo "<br/><br/><br/>";
          echo "<center>";
          echo "<p>No image selected/uploaded. <br/> Please click <a href='editHomepageImage.php'>here</a> to try again</p>";
          echo "</center>";
 
    }
    
    // Close our MySQL Link
    mysql_close($link);
    ?>      
 
It's returning a message saying that the image has successfully been uploaded, but it's not actually storing in the database..

Any suggestions as to why this might be??

Thanks so much

Dee


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Storing image in a database - won't save

Posted: Wed Feb 04, 2009 9:34 am
by onion2k
You aren't actually checking the query worked or not. Look to see if mysql_error() contains anything.

Re: Storing image in a database - won't save

Posted: Wed Feb 04, 2009 9:38 am
by tenacious-dee
Sorry now, I'm very new to php..

where should i put the mysql_error()??

Re: Storing image in a database - won't save

Posted: Wed Feb 04, 2009 9:40 am
by tenacious-dee
okay i've checked the results of the query..

Code: Select all

$query = "UPDATE tblhomepage SET image='$data' WHERE homepage_id=4";
          $results = mysql_query($query, $link);
         if (!$results) {
        die('Could not connect: ' . mysql_error());
 
And it doesn't seem to be working.. does that mean that the error is in my sql and not the php??

Re: Storing image in a database - won't save

Posted: Wed Feb 04, 2009 9:44 am
by VladSun
tenacious-dee wrote:And it doesn't seem to be working..
This will not help us understranding what you are doing wrong ;)

Post the error messages instead :)

Re: Storing image in a database - won't save

Posted: Wed Feb 04, 2009 9:47 am
by tenacious-dee
Sorry for being vague.. :)

I wrapped $results in the following:

Code: Select all

$query = "UPDATE tblhomepage SET image='$data' WHERE homepage_id=4;
          $results = mysql_query($query, $link);
         if (!$results) {
        die('Could not connect: image query not working ');
 
and it returns the 'could not connect: image not working message'... so i assume the prob is with my sql??

Re: Storing image in a database - won't save

Posted: Wed Feb 04, 2009 10:55 am
by VladSun
Post the output of your previous post :)

Re: Storing image in a database - won't save

Posted: Wed Feb 04, 2009 11:23 am
by tenacious-dee

Code: Select all

Could not connect: image query not working
The query is actually working fine in mysql..

It seems just when i'm sending it from the php page it's not working

Re: Storing image in a database - won't save

Posted: Wed Feb 04, 2009 11:25 am
by VladSun
tenacious-dee wrote:

Code: Select all

Could not connect: image query not working
The query is actually working fine in mysql..

It seems just when i'm sending it from the php page it's not working
So... post the error message from mysql_error() ...
It's not hard to do it ;)

I've already told you that explanations like "it's not working" will not going to help in any way.

Re: Storing image in a database - won't save

Posted: Wed Feb 04, 2009 11:28 am
by VladSun
Also, use mysql_real_escape_string() instead of addslashes()

Re: Storing image in a database - won't save

Posted: Wed Feb 04, 2009 11:30 am
by tenacious-dee
okay changed that.. still not storing

Re: Storing image in a database - won't save

Posted: Wed Feb 04, 2009 11:31 am
by tenacious-dee
if it helps this is the code creating the form to take in the image

Code: Select all

<form enctype="multipart/form-data" action="insertHomepageImage.php" method="post" name="changer">
            <tr><td><input name="MAX_FILE_SIZE" value="3000000000" type="hidden"></td></tr>
            <tr><td><input name="image" type="file"></td></tr>
            <tr><td><input value="Submit New Image" type="submit"></td></tr>
            </form>
 

Re: Storing image in a database - won't save

Posted: Wed Feb 04, 2009 11:53 am
by Benjamin
Storing images in the database is a bad idea. I really can't think of 1 good reason to do such a thing. I would encourage you to save the images to the file system instead.