| | |
Displaying images retrieved from a mysql db using the image path
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Solved Threads: 0
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:
Here is my bad attempt of the display page:
Can anyone help me as to where or what I am doing wrong here?
Thanks
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:
sql Syntax (Toggle Plain Text)
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 Syntax (Toggle Plain Text)
<?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:
php Syntax (Toggle Plain Text)
<? $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
Last edited by peter_budo; Jun 10th, 2008 at 7:30 am. Reason: Place the code between code tags [code]YOUR CODE HERE[/code]
•
•
Join Date: Jun 2008
Posts: 55
Reputation:
Solved Threads: 2
Look at this thread: http://www.daniweb.com/forums/thread39857.html
![]() |
Similar Threads
Other Threads in the PHP Forum
- Previous Thread: error in displaying results.
- Next Thread: determining if a checkbox is checked
| Thread Tools | Search this Thread |
advanced apache api array basics beginner binary broken cakephp check checkbox class cms code combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert ip javascript job joomla js limit link login mail menu mlm mobile multiple mysql oop outofmemmory paging parse password paypal pdf php problem procedure query radio random recursion regex remote script search server sessions smarty sms soap source space sql stored syntax system table traffic tutorial unicode up-to-date update upload url validator variable video web webapplications xml youtube





