由于SQL语法而出错

时间:2015-01-04 17:37:11

标签: php mysql

我正在创建一个将图像上传到服务器的表单和另一个从数据库中检索图像的文件。

错误:

  INSERT到'images_tbl'('images_path','submission_date')的错误

VALUES('images / 04-01-2015-1420392279.png','2015-01-04')==你有错误你的SQL语法;检查与MySQL服务器版本对应的手册,以便在''images_tbl'附近使用正确的语法('images_path','submission_date')VALUES('images / 04-01-2015-14'在第1行

的index.php:

<form action="saveimage.php" enctype="multipart/form-data" method="post">

<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5">
<tbody><tr>
<td>
<input name="uploadedimage" type="file">
</td>

</tr>

<tr>
<td>
<input name="Upload Now" type="submit" value="Upload Image">
</td>
</tr>

</tbody></table>

</form>

index2.php:

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

$select_query = "SELECT 'images_path' FROM  'images_tbl' ORDER by 'images_id' 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[" images_path"];="" ?="">" alt="" />">

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

<?php
}
?>

mysqlconnect.php:

<?php
/**********MYSQL Settings****************/
$host="localhost";
$databasename="karma";
$user="root";
$pass="";
/**********MYSQL Settings****************/

$conn=mysql_connect($host,$user,$pass);

if($conn)
{
$db_selected = mysql_select_db($databasename, $conn);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
}
else
{
    die('Not connected : ' . mysql_error());
}
?>

saveimage.php:

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

    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 'images_tbl' ('images_path','submission_date') 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");
} 

}

?>;

SQL:

CREATE TABLE images_tbl(
   images_id INT NOT NULL AUTO_INCREMENT,
   images_path VARCHAR(200) NOT NULL,
   submission_date DATE,
   PRIMARY KEY (images_id)
);

3 个答案:

答案 0 :(得分:3)

single quotestable name移除column list

INSERT into images_tbl (images_path,submission_date) 
         VALUES ('images/04-01-2015-1420392279.png','2015-01-04')

如果object名称与keywords类似,请使用backticks而不是single quotes

答案 1 :(得分:1)

不要把'images_tbl&#39;和引号之间的列名:正确的查询应该是

$query_upload="INSERT into images_tbl (images_path,submission_date) VALUES ('images/04-01-2015-1420392279.png','2015-01-04')

答案 2 :(得分:1)

我认为您已将后退标记单引号混淆

你应该将你的MySQL对象名称包装在后面,而不是单引号中

INSERT into `images_tbl` (`images_path`,`submission_date`) 
VALUES ('images/04-01-2015-1420392279.png','2015-01-04')