2016-08-06 43 views
0

當我嘗試與地方代碼中插入數據:我儘量讓地方插入SQL中,但它給我一個錯誤

$query = dbConnect()->prepare("INSERT INTO users(key) WHERE mail='$mail' VALUES ('$key')"); 

我使用的XAMPP,它給了我一個錯誤:

Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'key) WHERE mail='[email protected]' VALUES (key)' at line 1 in C:\xampp\htdocs\PHP7_login\restore\index.php:38

+1

它會,因爲SQL不允許[插入](http://dev.mysql.com/doc/refman/5.7/en/insert.html)與WHERE子句,除非你從一張桌子到另一個桌子 –

+1

'鑰匙'是一個保留字 –

+0

那裏,2次罷工。 –

回答

0

你應該使用反引號關鍵(因爲是保留字)
和地方使用

"INSERT INTO users(`key`) VALUES ('$key')" 

或者,如果你需要更新

"UPDATE users 
set `key` = '$key' 
where mail = '$mail'" 
+0

謝謝!我忘了那個 –

0

的猜測是,你要update

update users 
    set key = '$key' 
    where mail = '$mail' ; 

你也應該學會在查詢中使用的參數值。將字符串替換爲查詢字符串引入了意外錯誤的可能性,並使代碼易受SQL注入攻擊的影響。

相關問題