-2

Uploading an image to a folder in a server and saving the path to this image in a SQL database, I'm achieving the error below.

error in INSERT into 'offerstbl' ('ImagesPath','SubmissionDate') VALUES ('images/29-07-2014-1406647868.png','2014-07-29') == ----> You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''offerstbl' ('ImagesPath','SubmissionDate') VALUES ('images/29-07-2014-1406647' at line 1

PHP CODE

 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>

MySQL code

 INSERT INTO `offerstbl`(`ImagesId`, `ImagesPath`, `SubmissionDate`) VALUES ([value-1],[value-2],[value-3])
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
user3841443
  • 9
  • 1
  • 10

1 Answers1

1

Your query is incorrect.

Tables and column identifiers do not use quotes, but backticks or none at all; unless it contains a MySQL reserved word.

Plus, it contains a few stray double quotes " in both columns.

INSERT into 'offerstbl' ('ImagesPath"','"SubmissionDate') VALUES 
            ^         ^  ^          ^^ ^^              ^

Change it to:

INSERT into `offerstbl` (`ImagesPath`,`SubmissionDate`) VALUES ...

Thanks to the comments for spotting the ' out. I totally missed those.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49