从php 5改为php 7,我必须更改我的查询。现在使用PDO。
这种方法是否足以防止mysql注入等?
我像这样连接到数据库:
$conn = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);我的完整查询是由union加入的8个查询。我的(简化)查询是这样的:(还有其他的科尔需要查询,因此众多的联合所有)
$query="select * from product_catalogue
where sub_category like ?
union all
select * from product_catalogue
where brand like ?
union all
select * from product_catalogue
where category like ?
union all
select * from product_catalogue
where colour like ?
union all
select * from product_catalogue
where stock_code like ?
union all
select * from product_catalogue
where barcode like ?
union all
select * from product_catalogue
where item_name like ?
union all
select * from product_catalogue
where department like ?";
$input = "%".$input."%";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $input, PDO::PARAM_STR, 12);
$stmt->bindParam(2, $input, PDO::PARAM_STR, 12);
$stmt->bindParam(3, $input, PDO::PARAM_STR, 12);
$stmt->bindParam(4, $input, PDO::PARAM_STR, 12);
$stmt->bindParam(5, $input, PDO::PARAM_STR, 12);
$stmt->bindParam(6, $input, PDO::PARAM_STR, 12);
$stmt->bindParam(7, $input, PDO::PARAM_STR, 12);
$stmt->bindParam(8, $input, PDO::PARAM_STR, 12);
$stmt->execute();
$data = $stmt->fetchAll();发布于 2020-09-24 16:23:09
是的,这对于避免sql注入是可以的。
不过,在将所有%字符和下划线字符从$input中删除或转义用于查询之前,最好将其移除或转义。这样做可以避免在类似的结果中出现混乱。
https://stackoverflow.com/questions/64050296
复制相似问题