1 Replies - 1205 Views - Last Post: 21 January 2011 - 07:57 PM Rate Topic: -----

#1 mdmartiny  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 28-May 10

Php and MySql not displaying image from path in DB

Posted 21 January 2011 - 07:26 PM

I am trying to get an image to show up after it is submitted to the database for entry.

I have looked for days and I have tried many many possible solutions but nothing that I try works. The image and all of the information that is submitted is recorded in the database. It is just that the image does not show up when the information is being retrieved from the database

This is the code that I am using to add the info to the database
//check and see if the type of uploaded file is an image
function is_valid_type($file) {
	$valid_types = array("image/jpg", "image/jpeg", "image/gif", "image/bmp");
	
	if (in_array($file['type'], $valid_types))
		return 1;
	return 0;
}

//Set Constants
$TARGET_PATH = "/home/content/m/i/k/mikedmartiny/html/db_images/";

$title = $_POST['title'];
$year = $_POST['year'];
$make = $_POST['make'];
$model = $_POST['model'];
$descript = $_POST['descript'];
$image = $_FILES['image'];

//Sanitize the inputs
$title = mysql_real_escape_string($title);
$year = mysql_real_escape_string($year);
$make = mysql_real_escape_string($make);
$model = mysql_real_escape_string($model);
$descript = mysql_real_escape_string($descript);
$image['name'] = mysql_real_escape_string($image['name']);

//$target_path full string
$TARGET_PATH .= $image['name'];

//make sure that all fields from form are filled in
if ( $title == "" || $year == "" || $make ="" || $model == "" || $descript == "" || $image['name'] == "") {
	$_SESSION['error']  = "ALL FIELDS ARE REQUIRED!";
	header ("Location: show_add.php");
	exit;
}

//check to make sure it has the right file type
if (!is_valid_type($image)){
	$_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
	header ("Location: show_add.php");
	exit;
}

//check to see if a file with that name exsists
if (file_exists($TARGET_PATH)){
	$_SESSION['error'] = "A FILE WITH THAT NAME ALL READY EXIST!";
	header ("Location: show_add.php");
	exit;
}

//move the image - write path to database
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH)){
	$sql = "INSERT INTO $table (id, title, year, make, model, descript, image) VALUES ('', '$_POST[title]', '$_POST[year]', '$_POST[make]', '$_POST[model]', '$_POST[descript]', '" . $image['name'] . "')";
$result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());
} else {
	// Make sure you chmod the directory to be writeable
	$_SESSION['error'] = "COULD NOT UPLOAD FILE. CHECK WRITE/REWRITE PERMISSIONS ON THE FILE DIRECTORY!";
	header ("Location: show_add.php");
	exit;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Classified Added</title>
</head>
<body>
<div>
   <h3>The following information has been added to the <?php echo "$table"; ?></h3>
   <p> <strong>Title:</strong> <?php echo "$title"; ?> </p>
   <p> <strong>Year:</strong> <?php echo "$year"; ?> </p>
   <p> <strong>Make:</strong> <?php echo "$make"; ?> </p>
   <p> <strong>Model:</strong> <?php echo "$model"; ?> </p>
   <p> <strong>Description:</strong> <?php echo "$descript"; ?> </p>
   <p> <strong>Image:</strong> <?php  echo "<img src=\"../db_images/'$image'\">"; ?></p>
   <p><a href="show_add.php">Add another classified ad</a></p>
   <p><a href="admin_menu.php">Return to main menu</a></p>



This is the code output

   <p> <strong>Year:</strong> 1998 </p>
   <p> <strong>Make:</strong>  </p>
   <p> <strong>Model:</strong> camaro </p>

   <p> <strong>Description:</strong> kjhgnfbdvcscdvfbgnhmj,k.hmgnfbdvscxcdvfbgnhmj,k </p>
   <p> <strong>Image:</strong> <img src="../db_images/%27Array%27"></p>
   <p><a href="show_add.php">Add another classified ad</a></p>
   <p><a href="admin_menu.php">Return to main menu</a></p>



What am I doing wrong? Can someone please help me?

Is This A Good Question/Topic? 0
  • +

Replies To: Php and MySql not displaying image from path in DB

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 541
  • View blog
  • Posts: 1,708
  • Joined: 08-November 08

Re: Php and MySql not displaying image from path in DB

Posted 21 January 2011 - 07:57 PM

Line 78 from your first block of code:

<p> <strong>Image:</strong> <?php  echo "<img src=\"../db_images/'$image'\">"; ?></p>


$image is your $_FILES array. Also, why're you putting single quotes around it? You're probably looking for something more like this:

<p> <strong>Image:</strong> <?php echo "<img src=\"../db_images/" . $image['name'] . "\">"; ?></p>


I would highly advise breaking away from having PHP and HTML in the same pages though, and breaking in and out of PHP. It will make your code far, far harder to read and maintain (as you've noticed from how long it's been taking you to find this bug!).
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1