Photos in php

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
MikeFuller
Forum Newbie
Posts: 5
Joined: Wed Jul 12, 2006 9:25 am

Photos in php

Post by MikeFuller »

Hello all!!

I have a slight little problem with a page I'm making.. The LAST thing I have yet to do is be able to upload an image onto the server, save the path into a MySQL Database, then take the path out of the database and display the picture on the page. Why this normally wouldn't be hard to do, the pages are search engine friendly (http://www.sitepoint.com/article/search ... endly-urls). The problem I am having is that the path to the image IS stored in the database, but when I use this piece of code to pull it out:

Code: Select all

include_once("db.inc");
$link = mysql_connect($host,$user,$pass,true)
mysql_select_db($db);
$query = "SELECT * FROM SiteInfo WHERE InfoID = $pagetoload";
$result = mysql_query($query);
$Site = mysql_fetch_object($result,$link);
echo($Site->photopath);

echo"<img src=\"$Site->photopath\" alt=\"Featured Picture\" title=\"Featured Picture\"width=\"250\" height=\"165\" border=\"0\" style= \"border:\"3px\"\"/></a> <br />";
and the code to upload it:

Code: Select all

#####################
# PHOTO UPLOAD
####
print_r($_POST); //Testing only
unset($imagename);
if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;
if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";
$imagename = basename($_FILES['image_file']['name']);
echo $imagename;
if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";
if(empty($error))
{
$newimage = $imagename;
echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
(yes I know that the above block of code is horribly shown, but when I pasted it, it was VERY messed up)

But anyways.. the picture isn't displayed. If you need an example, go to one of the pages that I made and see for yourself:

http://www.nearnorth1.com/Business/Businesses2.php/8

I looked at the information for the bottom image (the top, smaller one by the number is a different method I tried to get the image), it says the adress is that of the script. I don't remember if it should say that.. So if anyone can help me, I'd really appreciate it!!

Mike


(note: The reason it says Creative on the page is because I used the first word that came to my head to fill in the form)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm not seeing where you insert the image filename into your database.
MikeFuller
Forum Newbie
Posts: 5
Joined: Wed Jul 12, 2006 9:25 am

Post by MikeFuller »

Sorry, the problem is solved.. Right after I tested it out again, maybe with a 1 minute gap, I got the notification you replied.. Sorry!

Here's bits and pieces (Business Form, then the actual script to generate the site from the database)

Code: Select all

#####################
		# PHOTO UPLOAD
		####
			unset($imagename);
			
			if(!isset($_FILES) && isset($HTTP_POST_FILES))
			$_FILES = $HTTP_POST_FILES;
			
			if(!isset($_FILES['image_file']))
			$error["image_file"] = "An image was not found.";
			
			
			$imagename = basename($_FILES['image_file']['name']);
			//echo $imagename;
			
			if(empty($imagename))
			$error["imagename"] = "The name of the image was not found.";
			
			if(empty($error))
			{
			$newimage = "images/" . $num . ".jpg";
			//echo $newimage;
			$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
			if(empty($result))
			$error["result"] = "There was an error moving the uploaded file.";
			}
			
			if(is_array($error))
			{
				while(list($key, $val) = each($error))
				{
				echo $val;
				echo "<br>\n";
				}
			}
		#Insert values
		$query = "INSERT INTO SiteInfo SET InfoID = '$num', InfoName = '$Title', SDesc = '$Desc',
					KeyW1 = '$Key1', KeyW2 = '$Key2', KeyW3 = '$Key3', KeyW4 = '$Key4', KeyW5 = '$Key5', KeyW6 = '$Key6', KeyW7 = '$Key7', KeyW8 = '$Key8', KeyW9 = '$Key9', KeyW10 = '$Key10', KeyW11 = '$Key11', KeyW12 = '$Key12', Title = '$Title', Addr = '$Addr', TollPhone = '$PhoneList[0]', LocalPhone = '$PhoneList[1]', Fax = '$PhoneList[2]', Email = '$Email', Website = '$Website', Major = '$Maj', MajorAspects = '$MajAspects', Minor = '$Min', Minor1 = '$MinAspects[0]', Minor2 = '$MinAspects[1]', Minor3 = '$MinAspects[2]', Minor4 = '$MinAspects[3]', photopath = '$newimage';";

And the script that puts the image:

Code: Select all

<?php
		echo $pagetoload;
		include_once("db.inc");
		$link = @mysql_connect($host,$user,$pass,true)
			or die("Could not connect to the database server!");
		
		mysql_select_db($db);
		$query = "SELECT * FROM SiteInfo WHERE InfoID = $pagetoload";
		$result = mysql_query($query);
		$Site = mysql_fetch_object($result,$link);
		echo($Site->photopath); //Testing
		echo"<img src=\"..\\images\\$pagetoload\"/></a> <br />";
?>

Mike
Post Reply