2014-10-07 48 views
0

我運行下面的SQL查詢在PHP想不通這有什麼錯我的SQL語法

try { 
     $sql = "INSERT INTO doc SET type = 1, 
     candID = :candID, 
     userID = ".$_SESSION['userid'].", 
     filename = ".$_FILES['file']['tmp_name'].", 
     date=date_format(curdate(), '%d/%m/%Y')"; 

     $s = $pdo->prepare($sql); 
     $s->bindValue(':candID', $_POST['candid']); 
     $s->execute(); 
    } 
    catch (PDOException $e) { 
     $error = 'Error adding doc: ' . $e->getMessage(); 
     include $errorpage; 
     exit(); 
    } 

而且我發現了以下錯誤:

Error adding doc: SQLSTATE[42000]: Syntax error or access violation: 1064 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 ':\xampp\tmp\phpD58B.tmp, date=date_format(curdate(), '%d/%m/%Y')' at line 5

我可以」不知道爲什麼我會得到這個錯誤。我錯過了我的語法有什麼問題嗎?

+0

編輯您的問題並添加打印出來的'$ sql'值。答案可能很明顯。提示:字符串常量應該用單引號括起來。 – 2014-10-07 11:51:12

+0

提示:要在SQL中查找語法錯誤,首先需要**查看您運行的SQL。 – 2014-10-07 11:55:07

回答

1

filename = ".$_FILES['file']['tmp_name'].",應該是filename = '".$_FILES['file']['tmp_name']."',

,使代碼看起來應該是用於UPDATE語句

try { 
    $sql = "INSERT INTO doc SET type = 1, 
    candID = :candID, 
    userID = ".$_SESSION['userid'].", 
    filename = '".$_FILES['file']['tmp_name']."', 
    date=date_format(curdate(), '%d/%m/%Y')"; 

    $s = $pdo->prepare($sql); 
    $s->bindValue(':candID', $_POST['candid']); 
    $s->execute(); 
} 
catch (PDOException $e) { 
    $error = 'Error adding doc: ' . $e->getMessage(); 
    include $errorpage; 
    exit(); 
} 
+0

當然,謝謝 – dlofrodloh 2014-10-07 12:10:43

1

SET。

$sql = "INSERT INTO doc (type, candID, userID, finame, date) VALUES (1, :candID, :userID, :filename, :date)"; 

$s = $pdo->prepare($sql); 
$s->execute(array(':candID' => $_POST['candid'], ':userID' => $_SESSION['userid'], ':filename' => $_FILES['file']['tmp_name'], ':date' => date('d/m/Y')); 
+0

在MySQL中,幾乎所有的東西都是......請參閱http://dev.mysql.com/doc/refman/5.6/en/insert.html,並參考頂部的第二個語法塊。 – 2014-10-07 12:38:55

+0

SET也適用於INSERT。在MySql和MSSql中使用它。 – Romeo 2014-10-07 18:59:24

+0

SET不是標準的SQL,但確定MySQL支持它。 但使用PDO準備,然後在查詢中添加變量..? – Martijn 2014-10-07 19:26:12

相關問題