colcar Posted June 8, 2008 Share Posted June 8, 2008 Hi, Can anyone help me here, I want to display an image using the path of the image and the image title from a mysql db? I can upload the info to the db, but I have trouble displaying the image. Here is the code: CREATE TABLE `trailer` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(64) NOT NULL, `model` text NOT NULL, `year` text NOT NULL, `price` text NOT NULL, `location` text NOT NULL, `path` text NOT NULL, PRIMARY KEY (`id`) ); <?php $db_host = 'localhost'; // don't forget to change $db_user = 'root'; $db_pwd = 'colum'; $database = 'colum'; $table = 'trailer';// use the same name as SQL table $password = 'kinefad';// simple upload restriction,// to disallow uploading to everyone if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); // This function makes usage of// $_GET, $_POST, etc... variables // completly safe in SQL queries function sql_safe($s){ if (get_magic_quotes_gpc()) $s = stripslashes($s); return mysql_real_escape_string($s); } // If user pressed submit in one of the forms if ($_SERVER['REQUEST_METHOD'] == 'POST'){ // cleaning title field $title = trim(sql_safe($_POST['title'])); $model = trim(sql_safe($_POST['model'])); $year = trim(sql_safe($_POST['year'])); $price = trim(sql_safe($_POST['price'])); $location = trim(sql_safe($_POST['location'])); $path = './images/'; if ($title == '') // if title is not set $title = '(empty title)';// use (empty title) string if ($_POST['password'] != $password) // cheking passwors $msg = 'Error: wrong upload password'; else { if (isset($_FILES['photo'])) { if (!isset($msg)) // If there was no error { // Preparing data to be used in MySQL query mysql_query("INSERT INTO {$table} SET title='$title',model='$model',year='$year',price='$price',location='$location',path='$path'"); $msg = 'Success: image uploaded'; } } elseif (isset($_GET['title'])) // isset(..title) needed $msg = 'Error: file not loaded'; // to make sure we've using // upload form, not form // for deletion if (isset($_POST['del'])) // If used selected some photo to delete { // in 'uploaded images form'; $id = intval($_POST['del']); mysql_query("DELETE FROM {$table} WHERE id=$id"); $msg = 'Photo deleted'; } } } ?> <html><head> <title>Administration Page</title> </head> <body> <?php if (isset($msg)) // this is special section for // outputing message { ?> <p style="font-weight: bold;"><?=$msg?> <br> <a href="<?=$PHP_SELF?>">reload page</a> <!-- I've added reloading link, because refreshing POST queries is not good idea --> </p> <?php } ?> <h1>Administration Page </h1> <h2>Uploaded images:</h2> <form action="<?=$PHP_SELF?>" method="post"> <!-- This form is used for image deletion --> </form> <h2>Upload new image:</h2> <form action="<?=$PHP_SELF?>" method="POST" enctype="multipart/form-data"> <label for="title">Title:</label><br> <input type="text" name="title" id="title" size="64"><br><br> <label for="model">Model:</label><br> <input type="text" name="model" id="model" size="64"><br><br> <label for="year">Year:</label><br> <input type="text" name="year" id="year" size="64"><br><br> <label for="price">Price:</label><br> <input type="text" name="price" id="price" size="64"><br><br> <label for="location">Location:</label><br> <input type="text" name="location" id="location" size="64"><br><br> <label for="photo">Photo:</label><br> <input type="file" name="photo" id="photo"><br><br> <label for="password">Password:</label><br> <input type="password" name="password" id="password"><br><br> <input type="submit" value="upload"> </form> </body> </html> Here is my bad attempt of the display page: <? $dbcnx = @mysql_connect("localhost", "root", "colum"); if (!$dbcnx) { echo( "connection to database server failed!" ); exit(); } if (! @mysql_select_db("colum") ) { echo( "Image Database Not Available!" ); exit(); } $result = @mysql_query("SELECT * FROM trailer WHERE id=1"); if (!$result) { echo("Error performing query: " . mysql_error() . ""); exit(); } while ( $row = mysql_fetch_array($result) ) { $title = $row["title"]; $path = $row["path"]; } ?> <html> <body> <img src="images/$title.JPG"> </body> </html> Can anyone help me as to where or what I am doing wrong here? Thanks Link to comment https://forums.phpfreaks.com/topic/109285-displaying-images-retrieved-from-a-mysql-db-using-the-image-path/ Share on other sites More sharing options...
webent Posted June 8, 2008 Share Posted June 8, 2008 Right off the bat, this looks like it wouldn't work... <img src="images/$title.JPG"> try ... <img src="images/<? echo $title; ?>.JPG"> Didn't really look at too much of the other. Link to comment https://forums.phpfreaks.com/topic/109285-displaying-images-retrieved-from-a-mysql-db-using-the-image-path/#findComment-560630 Share on other sites More sharing options...
colcar Posted June 8, 2008 Author Share Posted June 8, 2008 Hi, Thanks that works...It's not working the way I want though!! It is only showing the last image in the table. I want it show the image for each row in the DB in a table: See my very poor attempt of code: <? $dbcnx = @mysql_connect("localhost", "root", "colum"); if (!$dbcnx) { echo( "connection to database server failed!" ); exit(); } if (! @mysql_select_db("colum") ) { echo( "Image Database Not Available!" ); exit(); } $result = @mysql_query("SELECT * FROM trailer"); if (!$result) { echo("Error performing query: " . mysql_error() . ""); exit(); } while ( $row = mysql_fetch_array($result) ) { $title = $row["title"]; $path = $row["path"]; $model = $row["model"]; $location = $row["location"]; $price = $row["price"]; $year = $row["year"]; } ?> <html> <body> <div align="center"> <center> <table border="0" width="31%" height="336" cellspacing="0" cellpadding="0"> <tr> <td width="50%" height="243" align="center"> <p align="left"><img border="0" src="images/<? echo $title; ?>.JPG" width="305" height="243" align="center"></p> <table border="1" width="100%" bordercolor="#0000FF"> <tr> <td width="50%"><b>Name</b></td> <td width="50%"><? echo $title; ?></td> </tr> <tr> <td width="50%"><b>Model</b></td> <td width="50%"><? echo $model; ?></td> </tr> <tr> <td width="50%"><b>Year</b></td> <td width="50%"><? echo $year; ?></td> </tr> <tr> <td width="50%"><b>Price</b></td> <td width="50%"><span lang="EN-IE" style="mso-ansi-language:EN-IE">€<o:p> </o:p> </span><? echo $price; ?></td> </tr> <tr> <td width="50%"><b>Location</b></td> <td width="50%"><? echo $location; ?></td> </tr> </table> </td> <td width="100%" height="243" align="center"> <p align="left"><img border="0" src="images/<? echo $title; ?>.JPG" width="305" height="243"> </p> <table border="1" width="100%" bordercolor="#0000FF"> <tr> <td width="50%"><b>Name</b></td> <td width="50%"><? echo $title; ?></td> </tr> <tr> <td width="50%"><b>Model</b></td> <td width="50%"><? echo $model; ?></td> </tr> <tr> <td width="50%"><b>Year</b></td> <td width="50%"><? echo $year; ?></td> </tr> <tr> <td width="50%"><b>Price</b></td> <td width="50%"> <span lang="EN-IE" style="mso-ansi-language:EN-IE">€<o:p> </o:p> </span><? echo $price; ?></td> </tr> <tr> <td width="50%"><b>Location</b></td> <td width="50%"><? echo $location; ?></td> </tr> </table> </td> </tr> </table> </center> </div> </body> </html> Can anyone help me? Or point me in the right direction? Thanks Link to comment https://forums.phpfreaks.com/topic/109285-displaying-images-retrieved-from-a-mysql-db-using-the-image-path/#findComment-560769 Share on other sites More sharing options...
webent Posted June 9, 2008 Share Posted June 9, 2008 If you want it to print all of the results, then you need to put that section of your html structure in your while loop... while ( $row = mysql_fetch_array($result) ) { } Link to comment https://forums.phpfreaks.com/topic/109285-displaying-images-retrieved-from-a-mysql-db-using-the-image-path/#findComment-560814 Share on other sites More sharing options...
colcar Posted June 9, 2008 Author Share Posted June 9, 2008 Hi, Thanks webent...that wokred wonders... I'm wrecking my brain all day in order to find the solution to my next problem. I have 2 tables within 1 big tables, in the 2 tables, the image and other information appear. Now I have the same image showing in both of the 2 smaller tables (i.e. in table 1 and 2, image 1 is being displayed. How can I change my code to show image 1 in table 1 and image 2 in table 2? I have tried several different attempts, but yet to have some joy. Here is the latest code can anyone point me in the right direction? <? $dbcnx = @mysql_connect("localhost", "root", "colum"); if (!$dbcnx) { echo( "connection to database server failed!" ); exit(); } if (! @mysql_select_db("colum") ) { echo( "Image Database Not Available!" ); exit(); } $result = @mysql_query("SELECT * FROM trailer"); if (!$result) { echo("Error performing query: " . mysql_error() . ""); exit(); } while ( $row = mysql_fetch_array($result) ) { $title = $row["title"]; $path = $row["path"]; $model = $row["model"]; $location = $row["location"]; $price = $row["price"]; $year = $row["year"]; ?> <html> <body> <div align="center"> <center> <table border="0" width="31%" height="336" cellspacing="0" cellpadding="0"> <tr> <td width="50%" height="243" align="center"> <p align="left"><img border="0" src="images/<? echo $title; ?>.JPG" width="305" height="243" align="center"></p> <table border="1" width="100%" bordercolor="#0000FF"> <tr> <td width="50%"><b>Name</b></td> <td width="50%"><? echo $title; ?></td> </tr> <tr> <td width="50%"><b>Model</b></td> <td width="50%"><? echo $model; ?></td> </tr> <tr> <td width="50%"><b>Year</b></td> <td width="50%"><? echo $year; ?></td> </tr> <tr> <td width="50%"><b>Price</b></td> <td width="50%"><span lang="EN-IE" style="mso-ansi-language:EN-IE">€<o:p> </o:p> </span><? echo $price; ?></td> </tr> <tr> <td width="50%"><b>Location</b></td> <td width="50%"><? echo $location; ?></td> </tr> </table> </td> <td width="100%" height="243" align="center"> <p align="left"><img border="0" src="images/<? echo $title; ?>.JPG" width="305" height="243"> </p> <table border="1" width="100%" bordercolor="#0000FF"> <tr> <td width="50%"><b>Name</b></td> <td width="50%"><? echo $title; ?></td> </tr> <tr> <td width="50%"><b>Model</b></td> <td width="50%"><? echo $model; ?></td> </tr> <tr> <td width="50%"><b>Year</b></td> <td width="50%"><? echo $year; ?></td> </tr> <tr> <td width="50%"><b>Price</b></td> <td width="50%"> <span lang="EN-IE" style="mso-ansi-language:EN-IE">€<o:p> </o:p> </span><? echo $price; ?></td> </tr> <tr> <td width="50%"><b>Location</b></td> <td width="50%"><? echo $location; ?></td> </tr> </table> </td> </tr> </table> </center> </div> </body> </html> <? } Thanks Link to comment https://forums.phpfreaks.com/topic/109285-displaying-images-retrieved-from-a-mysql-db-using-the-image-path/#findComment-561319 Share on other sites More sharing options...
colcar Posted June 10, 2008 Author Share Posted June 10, 2008 Hi, Can anyone help me here? I still can't figure this out. Thanks Link to comment https://forums.phpfreaks.com/topic/109285-displaying-images-retrieved-from-a-mysql-db-using-the-image-path/#findComment-562317 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.