Displaying images retrieved from a mysql db using the image path

Reply

Join Date: Jun 2008
Posts: 3
Reputation: colcar2008 is an unknown quantity at this point 
Solved Threads: 0
colcar2008 colcar2008 is offline Offline
Newbie Poster

Displaying images retrieved from a mysql db using the image path

 
0
  #1
Jun 8th, 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:
  1. CREATE TABLE `trailer` (
  2. `id` INT(11) NOT NULL AUTO_INCREMENT,
  3. `title` VARCHAR(64) NOT NULL,
  4. `model` TEXT NOT NULL,
  5. `YEAR` TEXT NOT NULL,
  6. `price` TEXT NOT NULL,
  7. `location` TEXT NOT NULL,
  8. `path` TEXT NOT NULL,
  9. PRIMARY KEY (`id`)
  10. );

  1. <?php
  2. $db_host = 'localhost'; // don't forget to change
  3. $db_user = 'root';
  4. $db_pwd = 'colum';
  5. $database = 'colum';
  6. $table = 'trailer';// use the same name as SQL table
  7. $password = 'kinefad';// simple upload restriction,// to disallow uploading to everyone
  8. if (!mysql_connect($db_host, $db_user, $db_pwd))
  9. die("Can't connect to database");
  10. if (!mysql_select_db($database))
  11. die("Can't select database");
  12. // This function makes usage of// $_GET, $_POST, etc... variables
  13. // completly safe in SQL queries
  14. function sql_safe($s){
  15. if (get_magic_quotes_gpc())
  16. $s = stripslashes($s);
  17. return mysql_real_escape_string($s);
  18. }
  19. // If user pressed submit in one of the forms
  20. if ($_SERVER['REQUEST_METHOD'] == 'POST'){
  21. // cleaning title field
  22. $title = trim(sql_safe($_POST['title']));
  23. $model = trim(sql_safe($_POST['model']));
  24. $year = trim(sql_safe($_POST['year']));
  25. $price = trim(sql_safe($_POST['price']));
  26. $location = trim(sql_safe($_POST['location']));
  27. $path = './images/';
  28. if ($title == '') // if title is not set
  29. $title = '(empty title)';// use (empty title) string
  30. if ($_POST['password'] != $password) // cheking passwors
  31. $msg = 'Error: wrong upload password';
  32. else
  33. {
  34. if (isset($_FILES['photo']))
  35. {
  36. if (!isset($msg)) // If there was no error
  37. {
  38. // Preparing data to be used in MySQL query
  39. mysql_query("INSERT INTO {$table} SET
  40.  
  41. title='$title',model='$model',year='$year',price='$price',location='$location',path='$path'");
  42. $msg = 'Success: image uploaded';
  43. }
  44. }
  45. elseif (isset($_GET['title'])) // isset(..title) needed
  46. $msg = 'Error: file not loaded';
  47. // to make sure we've using
  48. // upload form, not form
  49. // for deletion
  50. if (isset($_POST['del'])) // If used selected some photo to delete
  51. { // in 'uploaded images form';
  52. $id = intval($_POST['del']);
  53. mysql_query("DELETE FROM {$table} WHERE id=$id");
  54. $msg = 'Photo deleted';
  55. }
  56. }
  57. }
  58. ?>
  59. <html><head>
  60. <title>Administration Page</title>
  61. </head>
  62. <body>
  63. <?php
  64. if (isset($msg)) // this is special section for
  65. // outputing message
  66. {
  67. ?>
  68. <p style="font-weight: bold;"><?=$msg?>
  69. <br>
  70. <a href="<?=$PHP_SELF?>">reload page</a>
  71. <!-- I've added reloading link, because
  72. refreshing POST queries is not good idea -->
  73. </p>
  74. <?php
  75. }
  76. ?>
  77. <h1>Administration Page
  78. </h1>
  79. <h2>Uploaded images:</h2>
  80. <form action="<?=$PHP_SELF?>" method="post">
  81. <!-- This form is used for image deletion -->
  82.  
  83. </form>
  84. <h2>Upload new image:</h2>
  85. <form action="<?=$PHP_SELF?>" method="POST" enctype="multipart/form-data">
  86. <label for="title">Title:</label><br>
  87. <input type="text" name="title" id="title" size="64"><br><br>
  88. <label for="model">Model:</label><br>
  89. <input type="text" name="model" id="model" size="64"><br><br>
  90. <label for="year">Year:</label><br>
  91. <input type="text" name="year" id="year" size="64"><br><br>
  92. <label for="price">Price:</label><br>
  93. <input type="text" name="price" id="price" size="64"><br><br>
  94. <label for="location">Location:</label><br>
  95. <input type="text" name="location" id="location" size="64"><br><br>
  96. <label for="photo">Photo:</label><br>
  97. <input type="file" name="photo" id="photo"><br><br>
  98. <label for="password">Password:</label><br>
  99. <input type="password" name="password" id="password"><br><br>
  100. <input type="submit" value="upload">
  101. </form>
  102. </body>
  103. </html>
  104.  

Here is my bad attempt of the display page:

  1. <?
  2. $dbcnx = @mysql_connect("localhost", "root", "colum");
  3.  
  4. if (!$dbcnx)
  5. {
  6. echo( "connection to database server failed!" );
  7. exit();
  8. }
  9.  
  10. if (! @mysql_select_db("colum") )
  11. {
  12. echo( "Image Database Not Available!" );
  13. exit();
  14. }
  15.  
  16. $result = @mysql_query("SELECT * FROM trailer WHERE id=1");
  17.  
  18. if (!$result)
  19. {
  20. echo("Error performing query: " . mysql_error() . "");
  21. exit();
  22. }
  23.  
  24. while ( $row = mysql_fetch_array($result) )
  25. {
  26. $title = $row["title"];
  27. $path = $row["path"];
  28. }
  29.  
  30. ?>
  31.  
  32.  
  33. <html>
  34.  
  35. <body>
  36.  
  37. <img src="images/$title.JPG">
  38.  
  39. </body>
  40.  
  41. </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]
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 55
Reputation: kvdd is an unknown quantity at this point 
Solved Threads: 2
kvdd kvdd is offline Offline
Junior Poster in Training

Re: Displaying images retrieved from a mysql db using the image path

 
0
  #2
Jun 9th, 2008
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC