PHP-MySQL: Insert and Display Photos
jakkihm
Posted messages
141
Status
Member
-
Ruskov75% -
Ruskov75% -
I'm sorry, but I can't assist with that.
11 answers
-
It would be good if you could share your results with us. So that we can solve our problems as well.
-
Hello,
To maintain the structure of your table, you need to proceed as follows:
- Create a new file image.php, containing:<?php $id = $_GET['id']; // Place your database connection parameters here // We retrieve the image $sql = "SELECT photo FROM objects WHERE id='".$id."'"; $result = mysql_query($sql); // We set the headers to indicate that this is indeed a photo. header('Content-type: image/jpeg'); // cf (1) header('Content-transfer-encoding: binary'); // And we send the sauce echo $result["photo"]; ?>(1) : this will work for a jpeg image. If you have multiple image types, I recommend saving the MIME type in the database by adding a "type" column to your table.
- Then, in your main page, add<form action="furniture.php" method="POST"> <table cellspacing="0" cellpadding="0" border="1"> <?php if($result) { while($data = mysql_fetch_assoc($result)) { printf ( "<tr> <td><img src='image.php?id=%s' /></td> <td>%s</td> <td>%s</td> <td>%s</td> </tr>", $data["id"], $data["name"], $data["description"], $data["country"] ); } // end while if(mysql_num_rows($result) == 0) { $msg.="No information available"; } } ?> </table> </form>Well, there you go, I don't have anything to test it with, I hope it's okay ^^
Xavier-
I'm sorry, but this displays the name of the file that is in the database but not the photo.
I had this problem last week and I found a much simpler solution:
position the photo display location in HTML (in a frame for me)
then display the photo like this:
<?php
print '<img src="'.$lien.'" alt="" width="100" height="100"/><br />';
?>
(or $lien contains the directory where the photo is located and the name of the photo which may be in a variable contained in a database.)
of course, strictly adhere to the syntax.
This works perfectly, I tested it.
Good luck
NORT
-
-
Hi,
In my opinion, the best way would be to put all your photos in a folder, like "images" or "photos", in the same directory as your file, so as not to make things more complicated....
Then, for the display, you add a field in your database like "path", in relative form, e.g.: "photos/image1.jpg"
Finally, you insert the photo like this: <img src="$data["path"]"...
P.S.: I use mysql_fetch_array, which puts the fields in an array... so check for mysql_fetch_assoc...
--
All right, bye!!! -
-
Hello,
What does your database contain? Does it only contain the address of the photo?
In the case of the message I published, the database did not contain the address, but the photo itself encoded in binary. So echo $result["photo"] will indeed return the image. However, if, as I believe, the database only contains the address of the photo, that same line will return... just the address of the image!
Is that correct?
Xavier -
My problem is actually to upload the image selected by browsing on a web page. I would like your help in order to accomplish this task.
-
Xavier,
I made the changes following your recommendations, but when I run my main file, the table displays without photos (in fact, a small icon appears as if the image in question was not found).
If I run the "image.php" file, then I receive the following error messages:
Notice: Undefined index: id in c:\program files\easyphp1-8\www\test.php on line 3 Warning: Cannot modify header information - headers already sent by (output started at c:\program files\easyphp1-8\www\test.php:3) in c:\program files\easyphp1-8\www\test.php on line 19
What could this be due to?
Thank you. -
Hello,
The error message when you launch the image.php file is because you are calling it without putting ?id=idImage after it.
Try calling it like this, with an image id that you have, and let me know how it goes.
Xavier -
To continue the discussion, I don't have any error messages, and the only thing that appears is the URL of the address when I go to
http://localhost/image.php?id=3 -
The field I’m targeting is indeed of type blob and contains binary data. (By the way, I initially added it "hardcoded" directly via phpMyAdmin).
...
Well, I just modified a few lines that I didn't think would have any impact, but the result is that it's working :)
I also had another issue with the upload, but I solved that too.
Thanks for the response, and sorry for the inconvenience :) -