-3

Trying the upload a images to a server and the path mySQL using PHP. Another PHP file then displays the image. However I get "Notice: Undefined variable" error for the following line and the image is displayed as a broken image:

<img src="<?php echo $row[" ImagesPath"]; ?>" />

Here's the code for uploading the image and the path.

 function GetImageExtension($imagetype)
 {
   if(empty($imagetype)) return false;
   switch($imagetype)
   {
       case 'image/bmp': return '.bmp';
       case 'image/gif': return '.gif';
       case 'image/jpeg': return '.jpg';
       case 'image/png': return '.png';
       default: return false;
   }
 }



if (!empty($_FILES['uploadedimage']['name'])) {

$file_name=$_FILES["uploadedimage"]['name'];
$temp_name=$_FILES['uploadedimage']['tmp_name'];
$imgtype=$_FILES['uploadedimage']['type'];
$ext= GetImageExtension($imgtype);
$imagename=date('d-m-Y').'-'.time().$ext;
$target_path = "images/".$imagename;


if(move_uploaded_file($temp_name, $target_path)) {

$query_upload="INSERT into `offerstbl` (`ImagesPath`,`SubmissionDate`) VALUES

 ('".$target_path."','".date("Y-m-d")."')";
 mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error());  

 }else{

 exit("Error While uploading image on the server");
 } 

 }

?>

</html>

Here's the mySQL code

CREATE TABLE offerstbl(
ImagesId INT NOT NULL AUTO_INCREMENT,
ImagesPath VARCHAR(200) NOT NULL,
SubmissionDate DATE,
PRIMARY KEY (ImagesId)
);

And here's the code for displaying the images.

 <!--?php
 include("mysqlconnect.php");

 $select_query = "SELECT `ImagesPath` FROM `offerstbl` ORDER by `ImagesId` DESC";
 $sql = mysql_query($select_query) or die(mysql_error());   
 while($row = mysql_fetch_array($sql,MYSQL_BOTH)){

 ?-->

 <table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5"   cellpadding="5">
 <tbody><tr>
 <td>



 <img src="<?php echo $row[" ImagesPath"]; ?>" />

 </td>
 </tr>
 </tbody></table>

 <!--?php


  }
 ?-->
user3841443
  • 9
  • 1
  • 10

1 Answers1

2

As the error said, $row is undefined.

Your query execution lines are not contained into php code. You commented the lines with en HTML comment (and you removed the PHP opening tag). I also removed a space before "ImagesPath".

Fixed code :

 <?php
 include("mysqlconnect.php");

 $select_query = "SELECT `ImagesPath` FROM `offerstbl` ORDER by `ImagesId` DESC";
 $sql = mysql_query($select_query) or die(mysql_error());   
 while($row = mysql_fetch_array($sql,MYSQL_BOTH)){

 ?>

 <table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5"   cellpadding="5">
 <tbody><tr>
 <td>



 <img src="<?php echo $row["ImagesPath"]; ?>" />

 </td>
 </tr>
 </tbody></table>

 <?php


  }
 ?>
Kevin Labécot
  • 2,005
  • 13
  • 25