PHP-执行功能不适用于爆破

时间:2018-07-22 22:08:23

标签: php sql pdo sql-injection

好的,
我不知道到底是什么问题。因此,我决定将其发布在这里与您讨论。
问题是,当我在 PDO execute(array(implode(",",$imploded))));中使用php implode函数时,它不起作用
当我在选择语句中使用php implode函数“具有相同变量的相同函数”时,它可以正常工作!
我怀疑在语句中使用它是否会成为 SQL注入的机会。

这是我的完整代码:

$exCat = explode(",", $article['Category']);
$getCats = $con->prepare("SELECT * FROM `Categories` WHERE `ID` IN (?)");
if (is_array($exCat)) {
    $getCats->execute(array(implode(",", $exCat))); /* This Is only displaying the first element */
} else {;
    $getCats->execute(array($exCat));
}
$getCATS = $getCats->fetchAll();

这对我很好。但是,我怀疑在语句中使用它是否会成为 SQL注入的机会。

$exCat = explode(",", $article['Category']);
$anotherStmt = $con->prepare("SELECT * FROM `Categories` WHERE `ID` IN (" . implode(",", $exCat) . ")"); /* This Works fine */
$anotherStmt->execute();
$anotherCATS = $anotherStmt->fetchAll();

1 个答案:

答案 0 :(得分:2)

explode在每个实例中都返回一个数组,因此不需要is_array

您需要为每个要绑定的值使用一个占位符。我会使用str_repeatrtrim来生成您的占位符,然后将展开的数组传递给execute。

$exCat = explode(",", 'non commaed list, commaed');
$placeholders = rtrim(str_repeat('?,', count($exCat)), ', ');
$getCats = $con->prepare("SELECT * FROM `Categories` WHERE `ID` IN ({$placeholders})");
$getCats->execute($exCat);
相关问题