我的网页上有页码。因此,如果我有来自分页的帖子,我想在查询中添加一行:
$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为空或和c.id < '$in‘,它将没有值。
它安全吗?
发布于 2016-05-04 21:56:14
在构建参数时,您总是可以像这样积累参数:
$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);我还没有测试这个,但它是基于这段代码的,可能需要一些调整才能工作。
PDO的execute()函数直接使用array,使用起来更容易。
https://stackoverflow.com/questions/37038429
复制相似问题