php - mysql isnumeric sql注入(inject)

标签 php mysql

我的网页有分页。因此,如果我有来自分页的帖子,我想在我的查询中添加一行:

$q="";
if(isset($_POST["inpi"])){
    $in = $_POST["inpi"];
    if(is_numeric($in)){
        $q = "and c.id < '$in'"; // add this to mysql
    }
    else{die("something is wrong!");}
}

所以我不能在这里使用准备语句。

select k.user, c.id, c.from, c.sent, c.message, c.recd from chat c
inner join cadastro k on c.from=k.id
where `from`=? and `to`=? $q

注意 $q 变量,如果 post 为空或者 and c.id < '$in',它将没有值。

它足够安全吗?

最佳答案

你总是可以像这样积累论据并构建它:

$query = "SELECT ... WHERE `from`=? AND `to`=?";
$binds = array("ss", &$from, &$to);

if (isset($_POST["inpi"])) {
  $query .= " AND c.id < ?";

  $$binds[0] .= "i";
  $binds[] = &$_POST["inpi"];
}

$stmt = $mysqli->prepare($query);

call_user_func_array(array($stmt, 'bind_param'), $binds);

我还没有测试过这个,但它是基于 this code 的并且可能需要一些调整才能工作。

PDO 的 execute() 函数直接接受一个 array,使用起来更容易。

关于php - mysql isnumeric sql注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37038429/

相关文章:

mysql - 如何在 MSSQL 中对单列使用 Group By

所有语言的 MySQL 排序规则

php - 如何在 MySQL 中转换包含 latin1 字符的 WordPress UTF-8 数据库

sql - 检查我的数据库设计/PHP/MySQL

php - 我的 UNION 查询中数组结果变为 0

php - 对某个 PHP 类的每个实例化对象执行一个方法

MySQL 距离关系的传递闭包

PHP 数组转换为 Json 并将数据恢复到网页中

php - 如果数据库中不存在行则显示链接,否则显示文本

php - Firestore : Key writes does not exist in the provided array