【发布时间】:2017-12-09 19:29:45
【问题描述】:
所以我看了这么多帖子、网站和视频,现在我很困惑!我似乎无法正确处理。 你如何在这个 PHP/PDO 中停止注入。我有这段代码有效,但它允许注入。
//*THIS WORKS BUT ALLOWS INJECTION
//*
//The variable $word comes from another php file where the search is created.
public function getAllCards($word) {
$sql = "SELECT * FROM carddbtable WHERE businessNameDB='".$word."'";
foreach ($this->conn->query($sql) as $row) {
echo json_encode($row)."<br>"."<br>";
}
$db = null;
}
使用这个新代码,我试图从“SELECT * FROM”语句中删除变量“$word” 停止注入并添加“准备”和错误检查以及“执行”语句,但我无法正确处理。我该怎么做?仅供参考,这是一个 GoDaddy 共享服务器。
//Getting the search "word" from the GetCards.php
public function getAllCards($word) {
//Empty var to store all returned info from db
$returnArray = array();
// sql statement to be executed
$sql = "SELECT * FROM carddbtable WHERE businessNameDB=':word";
// prepare to be executed
$statement = $this->conn->prepare($sql);
// error occurred
if (!$statement) {
throw new Exception($statement->error);
}
// execute statement
$statement->execute( :word => '$word' );
//run the query
foreach ($this->conn->query($statement) as $row) {
echo json_encode($row)."<br>"."<br>";
}
// store all appended $rows in $returnArray to be sent to app
$returnArray[] = $row;
}
【问题讨论】:
-
如果你运行你的代码,你应该会看到一个错误。阅读正确的语法in the manual