![](/img/trans.png)
[英]Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation PHP & PDO
[英]Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation:
致命錯誤:消息為“ SQLSTATE [42000]”的未捕獲的異常“ PDOException”:語法錯誤或訪問沖突:1064 SQL語法有錯誤; 在C:\\ xampp \\ htdocs \\ PhpProject1 \\ userv2.php:120中,檢查與您的MySQL服務器版本相對應的手冊,以在'“)VALUES()'的第1'行附近使用正確的語法:堆棧跟蹤:#0 C: \\ xampp \\ htdocs \\ PhpProject1 \\ userv2.php(120):PDOStatement-> execute()#1 C:\\ xampp \\ htdocs \\ PhpProject1 \\ create.php(41):User-> Save()#2 {main}拋出在第120行的C:\\ xampp \\ htdocs \\ PhpProject1 \\ userv2.php中
我們在列名中使用了反斜杠作為列名,這在此處的幾篇文章中已得到建議,但它不能完全解決錯誤。 我讀過類似的文章,但找不到解決方案,所以我在填寫錯誤和代碼以幫助您。 提前致謝。
<?php
/*
* mmber variable decleration...
*/
class User {
private $ID;
private $objPDO;
private $strTableName;
private $arRelationMap;
private $blForDeletion;
private $FirstName;
private $LastName;
private $Username;
private $Password;
private $EmailAddress;
private $DateLastLogin;
private $TimeLastLogin;
private $DateAccountCreated;
private $TimeAccountCreated;
//constructor is use for initialisation for the object
public function __construct(PDO $objPDO, $id = NULL) {
$this->strTableName = `system_user`;
$this->arRelationMap = array(
`id` => "ID",
`first_name` => "FirstName",
`last_name` => "LastName",
`username` => "Username",
`md5_pw` => "Password",
`email_address` => "EmailAddress",
`date_last_login` => "DateLastLogin",
`time_last_login` => "TimeLastLogin",
`date_account_created` => "DateAccountCreated",
`time_account_created` => "TimeAccountCreated");
$this->objPDO =$objPDO;
if (isset($id)) {
$this->ID = $id;
$strQuery = "SELECT ";
foreach ($this->arRelationMap as $key => $value) {
$strQuery .= "\"" . $key . "\",";
}
$strQuery = substr($strQuery, 0, strlen($strQuery)-1);
$strQuery .= "FROM" . $this->strTableName . " WHERE \"id\" = :eid";
$objStatement = $this->objPDO->prepare($strQuery);
$objStatement->bindparam(' :eid', $this->ID, PDO::PARAM_INT);
$objStatement->execute();
$arRow = $objStatement->fetch(PDO::FETCH_ASSOC);
foreach($arRow as $key => $value) {
$strMember = $this->arRelationMap[$key];
if(property_exists($this, $strMember)) {
if (is_numeric($value)) {
eval('$this->' . $strMember . ' = ' . $value . ';');
}
else
{
eval('$this->' . $strMember . ' = "' . $value . '";');
};
};
};
};
}
public function Save() {
if (!isset($this->ID)) {
$strValueList = "";
$strQuery = 'INSERT INTO "' . $this->strTableName . '"(';
foreach ($this->arRelationMap as $key => $value) {
eval('$actualVal = &$this->' . $value .';');
if(isset($actualVal)){
var_dump($actualVal);
$strQuery .= '"' . $key . '", ';
$strValueList .= ":$value, ";
} ;
}
$strQuery = substr($strQuery, 0, strlen($strQuery) - 2);
$strValueList = substr($strValueList, 0, strlen($strValueList) - 2);
$strQuery .= ") VALUES (";
$strQuery .= $strValueList;
$strQuery .= ")";
unset($objStatement);
$objStatement = $this->objPDO->prepare($strQuery);
foreach ($this->arRelationMap as $key => $value){
eval('$actualVal = &$this->' . $value . ';');
if (isset($actualVal)){
if ((is_int($actualVal)) || ($actualVal == NULL)) {
$objStatement->bindValue(':' . $value, $actualVal, PDO::PARAM_INT);
}
else
{
$objStatement->bindValue(':' . $value, $actualVal, PDO::PARAM_STR);
};
};
}
$objStatement->execute();
$this->ID = $this->objPDO->lastInsertId($this->strTableName . "_id_seq");
}
}
MySQL INSERT的正確語法是:
INSERT INTO tbl_name (col1,col2,col3) VALUES(15,col1*2,'string');
並不是 :
INSERT INTO "tbl_name" ("col1","col2","col3") VALUES(15,col1*2,'string');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.