我的网页有分页。因此,如果我有来自分页的帖子,我想在我的查询中添加一行:
$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/