Storing image in a database - won't save

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

Storing image in a database - won't save

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

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

Post by onion2k »

You aren't actually checking the query worked or not. Look to see if mysql_error() contains anything.
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

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

Post by tenacious-dee »

Sorry now, I'm very new to php..

where should i put the mysql_error()??
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

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

Post 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??
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post 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 :)
There are 10 types of people in this world, those who understand binary and those who don't
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

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

Post 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??
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post by VladSun »

Post the output of your previous post :)
There are 10 types of people in this world, those who understand binary and those who don't
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

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

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post by VladSun »

Also, use mysql_real_escape_string() instead of addslashes()
There are 10 types of people in this world, those who understand binary and those who don't
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

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

Post by tenacious-dee »

okay changed that.. still not storing
tenacious-dee
Forum Newbie
Posts: 19
Joined: Tue Jan 20, 2009 8:44 am

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

Post 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>
 
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

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

Post 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.
Post Reply