Page 1 of 1

Need major help with images in databases using PHP

Posted: Thu Mar 18, 2010 11:51 am
by cher41
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.


Hello all, I am a PHP newbie and needing some major help working with images in a mysql database using php.

Here is what I am wanting to do!

I have a membership website that I want to create a profile for each member. This profile page will come from a form that the member fills out. Obviously this info goes into a database and then is called later.

I have the form working and it is entering all the data correctly including the images. I am also able to get the data out of the database correctly except for the images!

I am able to get one image to display but then it ignores all the other code!

Here is the code I have:

This is the code I used to upload image into the database: As a blob.

Code: Select all

<?php
 
if(isset($_POST['upload']) && $_FILES['logo']['size'] > 0)
{
$fileName = $_FILES['logo']['name'];
$tmpName  = $_FILES['logo']['tmp_name'];
$fileSize = $_FILES['logo']['size'];
$fileType = $_FILES['logo']['type'];
 
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
 
if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
 
$mysqli = new mysqli("localhost", "XXX", "YYY", "voogahco_microblog");
 
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
 
} else {
 
$sql ="INSERT INTO logo (cust_id, file_name, file_size, file_type, content, date)
VALUES ('$_SESSION[$cust_id]', '$fileName', '$fileSize', '$fileType', '$content', NOW() )";
$res = mysqli_query($mysqli, $sql);
 
if ($res === TRUE) {
echo "";
} else {
printf ("Could not insert record: %s\n", mysqli_error($mysqli));
}
 
}
}
 
?>
 
 
Here is the code I have used to retrieve the images - this is not working. I have 4 images total that I need retrieved for the profile page.

Code: Select all

<?php
$username = "XXX";
$password = "YYY";
$host = "localhost";
$database = "voogahco_test";
 
@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
 
@mysql_select_db($database) or die("Can not select the database: ".mysql_error());
 
$id = 1;
 
if(!isset($id) || empty($id)){
die("Please select your image!");
}else{
 
$query = mysql_query("SELECT * FROM images WHERE image_id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['content'];
 
header('Content-type: image/jpg');
echo $content;
}
?> 
 
Obviously $id would equal the user_id.

Any help would be greatly appreciated!!


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: Need major help with images in databases using PHP

Posted: Thu Mar 18, 2010 12:25 pm
by JakeJ
Simple,

If you have an array of images, you have to loop through the array to get them.

Code: Select all

 
While ($row = mysql_fetch_array){
   header('Content-type: image/jpg');
   echo $row['content'];
}

Re: Need major help with images in databases using PHP

Posted: Thu Mar 18, 2010 12:34 pm
by cher41
Well no, I have each image stored into its own table.

The problem I having is that when I put the code to retrieve the image and call that file I get a bunch of "garbage" text.

But if I call that file from a different file using the following code:

<?

echo "<IMG SRC=\"http://www.voogah.com/includes/image.php\">";

?>

I get the image, and i can do this for multiple images and everything is grand!

But this only works if I manually put what the $id variable is, because with the file not being the original file called I am unable to pass the $id variable.

I am not sure if any of this makes since to anyone else, but I am not sure how else to describe it!

Re: Need major help with images in databases using PHP

Posted: Thu Mar 18, 2010 12:40 pm
by cher41
I am going to try to describe what I am working on again!

I have a members information in a database.

I have a search function in the website that pulls all the members from a specified category.

This creates a list and all of them are linked to a file called: profile?id=$id

This of course passed the correct $id to the file called profile.

I then have an "include" in the profile page that goes to my config file that gets the rest of the information once using the $id = $_GET(id).

This all works fine, I can get all the information except for the images. I have not found any code that works that will retrieve the images correctly.

The code I showed up in the original post just gives a bunch of "garbage" text if I enter it in the profile page and how would I call it if it was in the config file?

If I put it in a separate file it works by using the
<?

echo "<IMG SRC=\"http://www.voogah.com/includes/image.php\">";

?>
code but I have to manually enter the $ID variable, I am not sure how to pass this variable on otherwise. Obviously manually doing it doesn't work!

I hope this is a little more clear of what I am doing and what is wrong!

Re: Need major help with images in databases using PHP

Posted: Thu Mar 18, 2010 2:01 pm
by pickle
You will need to call your "image.php" file just like this:

Code: Select all

<img src = "http://www.voogah.com/includes/image.php?id=$id_of_person&number=1" alt = "image 1"/>
<img src = "http://www.voogah.com/includes/image.php?id=$id_of_person&number=2" alt = "image 2"/>
<img src = "http://www.voogah.com/includes/image.php?id=$id_of_person&number=3" alt = "image 3"/>
<img src = "http://www.voogah.com/includes/image.php?id=$id_of_person&number=4" alt = "image 4"/>

Re: Need major help with images in databases using PHP

Posted: Thu Mar 18, 2010 2:13 pm
by cher41
UNBELIEVABLE!!!!!!!

I have never been so happy and so angry at the same time!

Thank you Pickle!

I tried this in the very beginning several times but for some reason it didn't work. Obviously I had the code wrong somewhere else. After several attempts I assumed you couldn't pass a variable through the img src command and continued for days to find something else that would work!!

When I saw you posted it, I laughed and said, ya right but tried it anyway! And it worked like it is supposed to!

Thanks again!

Re: Need major help with images in databases using PHP

Posted: Thu Mar 18, 2010 3:28 pm
by JakeJ
Cher41,

One thing that you said concerns me. You said you have each image in a different table. Wouldn't it make a lot more sense to have all the images in one table with each image getting it's own id and another field for the customer id that the image belongs to? You'd get much better performance that way.

Re: Need major help with images in databases using PHP

Posted: Thu Mar 18, 2010 3:37 pm
by cher41
Yea, I need to rework the database!

Now that I have this under control I believe I will do that!

Thanks!!