首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这是正确的PDO和足以防止注射吗?

问这是正确的PDO和足以防止注射吗?
EN

Stack Overflow用户
提问于 2020-09-24 16:09:53
回答 1查看 52关注 0票数 0

从php 5改为php 7,我必须更改我的查询。现在使用PDO。

这种方法是否足以防止mysql注入等?

我像这样连接到数据库:

代码语言:javascript
复制
$conn = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);

我的完整查询是由union加入的8个查询。我的(简化)查询是这样的:(还有其他的科尔需要查询,因此众多的联合所有)

代码语言:javascript
复制
$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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-24 16:23:09

是的,这对于避免sql注入是可以的。

不过,在将所有%字符和下划线字符从$input中删除或转义用于查询之前,最好将其移除或转义。这样做可以避免在类似的结果中出现混乱。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64050296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档