-1
我遇到了一些问题,需要插入到我的数据库中。如果我将单引号或双引号放入我的文本字段,它将打破查询并且不会转义它们。我刚刚读完使用准备语句消除了调用mysql_real_escape_string的需要。有人可以告诉我,如果我执行我的查询错误。 $companyInfo
是一个包含约8行要插入的数组。使用准备好的语句,但引号不会被转义或删除
function InsertCompanyInfo($companyInfo, $conn) {
foreach($companyInfo as $key => $table) {
$keys = array_keys($table);
$values = null;
$x = 1;
foreach($table as $row => $value) {
$values .= "'$value'";
if($x < count($keys)) {
$values .= ', ';
}
$x++;
}
$sql = $conn->prepare("INSERT INTO {$key} (`" . implode('`, `', $keys) . "`) VALUES ({$values});");
$sql->execute();
$CompanyID = $conn->lastInsertId('CompanyID');
}
return $CompanyID;
}
这是错误,当我插入qoutes我得到:
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 '1',
''''"'"''"';;''';';')' at line 1' in /var/www/Survey/InsertFunctions.php:20 Stack trace: #0
/var/www/Survey/InsertFunctions.php(20): PDOStatement->execute() #1
/var/www/Survey/testProcess.php(8): InsertCompanyInfo(Array, Object(PDO)) #2 {main} thrown
in /var/www/Survey/InsertFunctions.php on line 20
这不是你如何使用预处理语句。你想在你的查询中使用占位符('?'),而不是直接拼接爆炸结果。 –
请仔细阅读如何使用预处理语句 –