簡體   English   中英

PDO INSERT語句出錯

[英]Error with PDO INSERT statement

我一直在盯着這個聲明及其錯誤信息大約半個小時而沒有看到它有什么問題。

這是我的發言:

try{
    $stmt = $conn->prepare("INSERT INTO dashboardsearchdates (userID, from, to) VALUES (?,?,?)");
    $result = $stmt->execute(array($userID, $frmFrom, $frmTo));
}
    catch(PDOException $e){
    echo 'ERROR: ' . $e->getMessage();
    $queryString = $stmt->queryString;
    $page = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    mail(ADMIN_EMAIL, 'SQL ERROR: ' . $page, 'Error Page: ' . $page . '     //     Error: ' . $e->getMessage() . '     //     Query String: ' . $queryString);
}

這是我試圖將值插入的表結構:

Table structure for table `dashboardsearchdates`

--

CREATE TABLE IF NOT EXISTS `dashboardsearchdates` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
  `userID` int(11) NOT NULL,
  `from` datetime NOT NULL,
  `to` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

以下是我看到的例外情況:

ERROR: 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 'from, to) VALUES ('9','2012-12-12','2013-01-01')' at line 1

從異常中可以看出,我試圖插入到DB中的三個值在運行時出現(否則它們不會顯示在異常中)

我試過的事情:

我在代碼中使用$conn以上的其他查詢,所以我知道$conn存在並正常工作。

我已經重寫了這句話:

$stmt = $conn->prepare("INSERT INTO dashboardsearchdates (userID, from, to) VALUES (:userID,:from,:to)");
$stmt->bindParam(':userID', $userID);
$stmt->bindParam(':from', $frmFrom);
$stmt->bindParam(':to', $frmTo);                        
$result = $stmt->execute();

我已經嘗試圍繞表名稱,所有字段名稱,只有整數字段名稱,只有日期字段的反引號。所有上面顯示相同的錯誤消息 - 據我所知,構造沒有任何問題SQL。

我真的很感激任何有關其他事情的建議。

'from'是一個關鍵詞,你需要將它包含在反引號中

`from`

'to'也是一個關鍵字

注意:此問題與PDO無關。 在開始動態構建之前,開發人員應該在mysql客戶端中測試他們的查詢。

From是SQL的保留字,所以如果你想使用它,你必須使用前后滴答。

INSERT INTO dashboardsearchdates (userID, `from`, to) VALUES (?,?,?)"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM