我想知道为什么在我准备的语句中,公式用户输入的一些字符没有像我预期的那样转义
if( isset( $_POST['key'] ) && strlen($_POST['key']) <= 15) {
$sql = "SELECT titel, date, content FROM News WHERE content LIKE :content OR titel LIKE :title";
if( $stmt = $pdo->prepare($sql) ) {
$temp = "%".$_POST['key']."%"; // NOT manually escaped here
$stmt->bindParam(':content', $temp); // should escape!?
$stmt->bindParam(':title', $temp); // should escape!?
$stmt->execute();
$stmt->bindColumn("Titel", $title, PDO::PARAM_STR);
$stmt->bindColumn("Datum", $date, PDO::PARAM_STR);
$stmt->bindColumn("Inhalt", $content, PDO::PARAM_STR);
while( $stmt->fetch() ) {
echo "<span class='head'>".$title." :: ".$date."</span><br />".shorten($content)."...<br /><hr>"; // the function shorten just shortens the content for preview reasons
}
// ends statement
$stmt = NULL;
// ends connection
$pdo = NULL;
}
else {
$err .= "statement wasn't prepare()'ed!";
}
}
else {
$err .= "no or false input!";
}
所以这基本上可以正常工作,但当我输入";"时例如,它只是抛出所有结果。所以我不确定它是否真的正确地逃脱了输入。我是错过了什么,还是不是所有的角色都逃脱了?如果是,它们是哪一个?我宁愿手动逃离它们。
我想知道为什么在我准备好的语句中,公式用户输入的一些字符没有像我预期的那样转义
因为你的期望是错误的
准备好的语句不一定涉及任何转义
即使是这样,一个诚实的分号字符也是完全无害的,并且根本不需要转义。
PDO/MySQL准备语句注入
您的代码中没有可能的注入,这是完全安全的。
当我输入";"时例如,它只是抛出所有结果。
这是另一个问题,与注射和逃跑无关。仔细检查您的数据和其他前提。
顺便说一下,把你的代码缩短一点
$sql = "SELECT titel, date, content FROM News WHERE content LIKE ? OR titel LIKE ?";
$temp = "%".$_POST['key']."%";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($temp,$temp));
while( $row = $stmt->fetch() ) {
extract($row);
echo "<span class='head'>$titel :: $date</span><br />".shorten($content)."...<br /><hr>";
}