2014-12-05 59 views
2

我對上傳圖像到服務器腳本工作,以及路徑MySQL數據庫。當我提出這一點,想出了這個錯誤:錯誤在INSERT

error in INSERT into 'images_tbl' ('images_path') VALUES ('images/05-12-2014-1417785023.png') == ----> 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 ''images_tbl' ('images_path') VALUES ('images/05-12-2014-1417785023.png')' at line 1

下面是代碼:

<?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') VALUES ('".$target_path."')"; 
    mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error()); 

}else{ 

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

} 

?> 

我的編輯是不是帶來了任何語法錯誤,但它似乎表明存在這樣的錯誤。

+0

在錯誤陳述,它的一個MySQL錯誤 – Grumpy 2014-12-05 13:17:09

+0

@Alex沃克 - 英厄姆不要使用mysql_,改用mysqli_ *或PDO。 – 2014-12-05 13:27:34

回答

2

標識符引號反引號不是單引號:

INSERT into 'images_tbl' ('images_path') 
      ^  ^   ^

你可以只溝它們來代替。

INSERT into images_tbl (images_path) 
// or 
INSERT into `images_tbl` (`images_path`) 

強制性注:

Please, don't use mysql_* functions in new code . They are no longer maintained and are officially deprecated . See the red box ? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial .

Ref: https://stackoverflow.com/a/12860140/3859027

下面是一個使用mysqli的一個簡單的例子:

$db = new mysqli('localhost', 'username', 'password', 'database'); 
$query_upload = 'INSERT INTO images_tbl (images_path) VALUES (?)'; 
$insert = $db->prepare($query_upload); 
$insert->bind_param('s', $target_path); 
$insert->execute(); 
2

它應該是 -

"INSERT into images_tbl (images_path) VALUES ('".$target_path."')"; 

OR

"INSERT into `images_tbl` (`images_path`) VALUES ('".$target_path."')"; 

取出'秒。這不是必需的。

2

您指定表名作爲簡單的字符串。用'替換'或者刪除它。

INSERT into `images_tbl` (`images_path`) VALUES 
1

您指定表名作爲字符串。

$query_upload="INSERT into 'images_tbl' ('images_path') VALUES ('".$target_path."')"; 

嘗試以下

$query_upload="INSERT into images_tbl ('images_path') VALUES ('".$target_path."')"; 

希望這段代碼可以幫助你。