Page 1 of 1

Php and MySql not displaying image from path in DB

Posted: Thu Jan 20, 2011 11:05 pm
by mdmartiny1977
Hello Everyone,

I hope someone will be able to help me with this since I am :banghead: banging my head against the wall. I am running a script that uploads information and image paths to a database. After the information has been uploaded to the database. It is retrieved to show the person what information has been added to the database.

I have everything else working I am just not able to get the image to display like it should. I have been trying to figure this out for a couple of days now and I am at the end of my rope.

The code that I am using is

Code: Select all

<?php

//Check to make sure title is filled in. If not redirects to show_add.php
if (!$_POST[title]) {
	header("Location: show_add.php");
	exit;
} else {
	//check and see if a session has started
	session_start();
}
	//if session has not been properly started redirects back to the administration menu
if ($_SESSION[valid] != "yes") {
	header("Location: admin_menu.php");
	exit;
}

include('includes/connection.php');


//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=\"images/" . $row['image'] . "\" alt=\"\" /><br />"; ?></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>
</div>
</body>
</html>
If someone one could help me or get me pointed in the right direction I would appreciate it. I am lost.

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

Posted: Fri Jan 21, 2011 1:47 am
by social_experiment
If you view the source code of the page where the image should be displayed, what does it look like, specifically the <img> tag

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

Posted: Fri Jan 21, 2011 2:03 am
by mehran
hi
please explan where you need help plz remove the emaining code then it could be better for help

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

Posted: Fri Jan 21, 2011 7:13 am
by mdmartiny1977
This is what the generated source code looks like

Code: Select all

<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 classified</h3>
   <p> <strong>Title:</strong> Image Test 3 </p>

   <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>

</div>
I don't want it to show up as an Array. I just want it to display the image that has been uploaded with the information that they are inputting

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

Posted: Sat Jan 22, 2011 12:37 am
by social_experiment

Code: Select all

<p> <strong>Image:</strong> <?php  echo "<img src=\"images/" . $row['image'] . "\" alt=\"\" /><br />"; ?></p>
Where do you get $row['image'] from? No where in the code (that you pasted) do you assign this value. Try substituing it with $image['image'] which contains the name of the file. Also, do this with $row['image'] to see what is inside.

Code: Select all

 print_r($row['image']);
Hth