我正在從正常的SQL遷移到PDO,因爲我讓我的一個朋友測試我是否有任何弱點,並且他建議我給PDO,因爲他發現很多弱點。致命錯誤:帶有消息'SQLSTATE [42000]的未捕獲異常'PDOException'
因此,這裏是我的全部錯誤:
Fatal error: Uncaught exception 'PDOException' with message '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 ' in /home/ubuntu/workspace/post.php on line 54
(!) PDOException: 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 '? (
id
,title
,info_bys
,info_shorts
,info_longs
,
這裏是我的代碼:
$stmt = $db->prepare("INSERT INTO :portal
(`id`, `title`, `info_bys`, `info_shorts`, `info_longs`, `email`, `filename`, `filepath`, `filename2`, `filepath2`, `approved`)
VALUES ('', ':title', ':by_information', ':short', ':long_information', ':email', ':filename', ':filetarget', ':filename2', ':filetarget2', 'false'");
$stmt->execute(array(':portal' => $portal, ':title' => $title, ':by_information' => $by_information, ':short' => $short, ':long_information' => $long_information, ':email' => $email, ':filename' => $fileName, ':filetarget' => $fileTarget, ':filename2' => $fileName2, ':filetarget2' => $fileTarget));
echo $affected_rows.' were affected';
有什麼我不能在PDO使用,我可以在SQL中使用還是我只是打字錯誤的東西。
希望有人能幫忙。
編輯:
新代碼:
的代碼function buildQuery($get_var)
{
switch($get_var)
{
case 1:
$portal = $_POST['portal'];
break;
}
$stmt = $db->prepare("INSERT INTO :portal
(`id`, `title`, `info_bys`, `info_shorts`, `info_longs`, `email`, `filename`, `filepath`, `filename2`, `filepath2`, `approved`)
VALUES (:title, :by_information, :short, :long_information, :email, :filename, :filetarget, :filename2, :filetarget2, 'false'");
$stmt->execute(array(':portal' => $portal, ':title' => $title, ':by_information' => $by_information, ':short' => $short, ':long_information' => $long_information, ':email' => $email, ':filename' => $fileName, ':filetarget' => $fileTarget, ':filename2' => $fileName2, ':filetarget2' => $fileTarget));
echo $affected_rows.' were affected';
}
表名不能綁定。你可以讓一個白名單可以比較名稱,然後通過它。 – chris85
Ow yea我明白了。我只是一個愚蠢的傻瓜。謝謝 –