Hey everyone,

Sorry for the lack of knowlege. I've tried google-ing how to imput and display images on a web page without the image showing up in weird symbols, but I've had no luck. Can anyone help me? My purpose is to simply store an image manually into a mysql database table and have that image or set of images display on a web page. This seems simple but I've struggled with the concept. Anyone have any suggestions and could elaborate so I can understand clearly what you all are trying to tell me? Thanks in advance!

Member Avatar for LastMitch

Sorry for the lack of knowlege. I've tried google-ing how to imput and display images on a web page without the image showing up in weird symbols, but I've had no luck. Can anyone help me? My purpose is to simply store an image manually into a mysql database table and have that image or set of images display on a web page. This seems simple but I've struggled with the concept. Anyone have any suggestions and could elaborate so I can understand clearly what you all are trying to tell me? Thanks in advance!

You can read these 2 links which has a code snippet you can used freely:

http://phpdevteam.com/blog/2010/01/display-stored-image-from-mysql-database-using-php/

http://www.phpfresher.com/tag/storing-images-in-databse/

Ok so I have a table in my database called 'markers' that hold street addresses and longitude and latitude coordinates to display on a map. I want to manually add an image for each address listed in the table and output the image properly along with the other data in the database..Looking at the links, I don't see how I can incorperate this process without totally changing everything and redoing my table...

Member Avatar for LastMitch

I want to manually add an image for each address listed in the table and output the image properly along with the other data in the database..

At first I thought you want to learn how to fetch and display an image from the database.

For Google map it's a bit different you need to do this first:

https://developers.google.com/maps/articles/phpsqlajax_v3

Then you need to do this too:

http://www.google.com/earth/outreach/tutorials/mapseng_upload.html

You might get some bugs if you run the code because it's was last updated 2007.

@LastMitch, That is what I want to do. I've got the whole map thing figured out. I've got markers that display the locations from a database I created. The original issue was how do I manuelly put images or image files (as suggested via google search) into database and output the images on a web page in the same query as to display the addresses? If it would help, I could put my table structure from my database on here..

Member Avatar for LastMitch

The original issue was how do I manuelly put images or image files (as suggested via google search) into database and output the images on a web page in the same query as to display the addresses?

You want manually put images into the database out put the image on the website?

I heard of doing something like

http://www.google.com/earth/outreach/tutorials/mapseng_upload.html

but not what you mention.

These are the tutorials I have look at and used before:

http://www.google.com/earth/outreach/tutorials/all.html#maps

Do you have a google earth outreach account? If you do then try those tutorials because I haven't seen any tutorial regarding about fetching images from the database and display the image on the google map before. It's usually display upon uploading the images manually(not from database).

@LastMitch, No, I don't want to display images on any map, I just want to create a list of the addressess and the images that correspond to those addresses to the side of the map so they display next to the map. I don't need the pictures to be on the map. I already have markers from the data in the database with latitude and longitude that display where places are on the map. The only thing I want to know how to do is to retrieve pictures from database and echo them out on the screen. I think I've figured out how to manually input them in using the 'blob' type. I just need to translate the binary data which is the 'blob' type into regular picture format. That is what I am trying to accomplish :)

Member Avatar for LastMitch

The only thing I want to know how to do is to retrieve pictures from database and echo them out on the screen.

This is your query to fetch the image from the data:

$stmt = $conn->query("SELECT * FROM Table WHERE id = '$id'");

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

echo '<img src="<?php stripslashes($row["text"]); ?>" width="50" height="79" border="0">';

}

The text should contain the name of the file so when you echo it out it will echo out the text from the database another words it will echo out the image.

The way I'm reading this you are storing the actual image in the database, much easier to store the images in a file, say 'map_images' and the image location in the db, as LastMitch's code. So in your db you have img 1 for id and map_images/img 1 for the location.

so would I just add the fields "map_images and "location" to a query? I'm sorry I'm not very proficiant in using images or image files and storing the location and then displaying them on the page..

What you have to do is; U create a dirctory in your project folder, and call it say "uploads", then in your database you will have two fields, one for the image_name and id; which links to the images in the directory.

Hope that helps

@Webville312, After I do that..How would I manually put those I guess file paths or information into the database without creating a form to upload them via php? The reason I ask this is because I want to make sure those images..paths or whatever goes to the correct field that displays a specific address so I can display that image with that street address.

Member Avatar for LastMitch

How would I manually put those I guess file paths or information into the database without creating a form to upload them via php?

I thought you already done that? You need a upload form for the data to be upload to the database. Then you can echo out the image.

This is how the table should look like:

id   | Address   |  City     | State   | Zip code
-----|-----------|-----------|---------|---------
 1   | image 1   |  image 2  | image 3 | image 4

This is your query:

$stmt = $conn->query("SELECT * FROM Table WHERE id = '$id'");
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<img src="<?php stripslashes($row["Address"]); ?>" width="10" height="10" border="0"></br>';
echo '<img src="<?php stripslashes($row["City"]); ?>" width="10" height="10" border="0"></br>';
echo '<img src="<?php stripslashes($row["State"]); ?>" width="10" height="10" border="0"></br>';
echo '<img src="<?php stripslashes($row["Zip code"]); ?>" width="10" height="10" border="0">';
}

If you need to design on how each image or where to put it then you do something like this seperately:

$stmt = $conn->query("SELECT * FROM Table WHERE Address = 'image 1'");
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<img src="<?php stripslashes($row["Address"]); ?>" width="10" height="10" border="0"></br>';
}

As you can see it's 1 image that is being echo out for that position (whatever position you are planning to do).

This code has nothing to do with Google Map. You are pretty much on your own with this because you want this be on Google Map. The code works.

@Webville312, what will the specifications be for the "image_name" field have? like "type" ect.. sorry I'm fairly new to this still..

Ok, maybe this will help clear some things up. This is my database structure:

CREATE TABLE IF NOT EXISTS `markers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(60) NOT NULL,
  `address` varchar(80) NOT NULL,
  `lat` float(10,6) NOT NULL,
  `lng` float(10,6) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=29 ;

I wanted to add an image field within this so that there is "One" image for each of what is displayed above. I don't want an image displayed for each field listed. Does this help?

Member Avatar for LastMitch

I wanted to add an image field within this so that there is "One" image for each of what is displayed above. I don't want an image displayed for each field listed. Does this help?

Then you just add image_name:

id  | name | address | lat | lng | image_name
----|------|---------|-----|-----|-----------

That's it there nothing more you need to add. Like what TonyG & WebVille mention before it's just that. That will work.

Do I have to set it to something in the database? for example the type or anything else?

@Anyone, Do I have to set the type or any other field or row to anything for the "image_name" field?

Member Avatar for LastMitch

Do I have to set the type or any other field or row to anything for the "image_name" field?

You can set it at varchar or a text for image_name

So to upload to the database, would I do something like this?

<?php
$hostname = "localhost";
$db_user = "username"; 
$db_password = "password";
$database = "database";
$db_table = "table";

$db = mysql_connect($hostname, $db_user, $db_password); 
mysql_select_db($database,$db);
$uploadDir = 'images/';
if(isset($_POST['Submit']))
{
$fileName = $_FILES['Photo']['name'];
$tmpName  = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
    $filePath = addslashes($filePath);
}
$query = "INSERT INTO $db_table ( image_name ) VALUES ('$filePath')";
mysql_query($query) or die('Error, query failed'); 
}
?>

...and how would I use the query to just update the images table without changing or adding a new row of data? I know I would probably use the "Update" command but I'm not sure what I would use to specify what I want to do..

?

anybody?

Would the query for the image location to be inserted into that database look something like this? "INSERT INTO tablename WHERE Image_name=$img_file AND id=1" ?

If you're trying to insert data then change the above query to:

"INSERT INTO `tablename` (`image_name`) values('$img_file')"

This will create a new entry in the table. While if you want to update an existing row, for example the row with id 1, use an update statement:

"UPDATE `tablename` SET `image_name` = '$img_file' WHERE `id` = 1"

Check MySQL manual for more information:

@cereal, Would the script I posted on page one of the post work for uploading image file location with the second query because thats the one I really am looking for.

Yes, it should work fine:

"UPDATE `tablename` SET `image_name` = '$filePath' WHERE `id` = 1"

However I prefer to save only the filename, because the path it can be an internal directory structure, it can change, and because the same name can used to point to different directories (images, thumbs100x100, thumbs200x200, ...) and retrieve different versions of the image, so I would use $fileName instead of $filePath:

"UPDATE `tablename` SET `image_name` = '$fileName' WHERE `id` = 1"

@cereal, Good point! Thanks! One more question..For the "image_name" field in the db, are there any parameters I need to set it to in order for the script to work? like setting the type or anything else?

No, once you have defined the image_name in the table structure, as suggested by LastMitch, MySQL will handle the input and return an error if something went wrong. So you don't need to set anything else to make the update query work.

When in doubt use mysql_error():

mysql_query($query) or die('Error, query failed: '. mysql_error());

It will help to understand errors.

@cereal, I tried uploading an image in the form, and nothing happened. It just refreshed the page.

Show us the form and the current script.

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.