代码之家  ›  专栏  ›  技术社区  ›  Hema_Elmasry

php-execute函数不能用于内爆

  •  0
  • Hema_Elmasry  · 技术社区  · 7 年前

    可以,
    我不知道到底是什么问题。所以,我决定把它贴在这里和你讨论。
    问题是,当我在 PDO公司 execute(array(implode(",",$imploded)))); 它不起作用
    当我在select语句中使用php内爆函数“具有相同变量的相同函数”时,它正常工作!
    我怀疑把它用在手术台上 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 回复  |  直到 7 年前
        1
  •  2
  •   user3783243    7 年前

    explode 在每个实例中返回一个数组,以便 is_array 不需要。

    需要为要绑定的每个值使用占位符我会用 str_repeat rtrim 要生成占位符,只需将分解数组传递给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);