2013-10-28 119 views
1

我是新來的PDO,我只是寫一個測試頁,從簡單的形式採取$ _POST數據,並通過準備好的語句插入到MySQL,但我不斷收到錯誤消息。這裏是PHP:PDO準備語句語法警告

$DBH = new PDO("mysql:host=localhost;dbname=randomDB", 'user', 'password'); 
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 

$newUserStmt = $DBH->prepare("INSERT INTO 'userbasicinfo' ('email', 'passHash', 'birthday', 'phoneNumber') VALUES (?, ?, ?, ?)"); 
$newUserStmt->bindParam(1, $email); 
$newUserStmt->bindParam(2, $passHash); 
$newUserStmt->bindParam(3, $birthday); 
$newUserStmt->bindParam(4, $phoneNumber); 


$email = $_POST['email']; 
$passClear = $_POST['password']; 
$passHash = password_hash($passClear, PASSWORD_DEFAULT); 
$birthday = $_POST['birthday']; 
$phoneNumber = $_POST['phone']; 
$newUserStmt->execute(); 

$DBH = null; 

這是警告:

警告:PDOStatement對象::執行():SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL錯誤句法;在''userbasicinfo''('email','passHash','birthday','phoneNumber')附近檢查與你的MySQL服務器版本對應的正確語法的手冊。VALUES('billy @'在C: \ XAMPP \ htdocs中上線\ test.php的18個

+0

你不能引用你的表名或表列,因爲當你這樣做時,它期望一個字符串,丟掉引號或使用反引號。 phoneNumber)'(謝謝邁克爾)。 –

+3

不只是_「不需要」_,而是_cannot_。請參閱http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks –

+0

順便說一句,它不屬於PDO –

回答

4

使用反引號,而不是單爲quotationmarks表名(和COLUMNNAMES):

$newUserStmt = $DBH->prepare("INSERT INTO `userbasicinfo` (`email`, `passHash`, `birthday`, `phoneNumber`) VALUES (?, ?, ?, ?)"); 

隨着單quotationsmarks數據庫,服務器interpretate表名作爲字符串