Php and MySql not displaying image from path in DB
Posted: Thu Jan 20, 2011 11:05 pm
Hello Everyone,
I hope someone will be able to help me with this since I am
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
If someone one could help me or get me pointed in the right direction I would appreciate it. I am lost.
I hope someone will be able to help me with this since I am
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>