用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3635|回复: 5
收起左侧

[版本 2.x] 这样写存在sql注入风险吗

[复制链接]
发表于 2013-7-6 10:14:32 | 显示全部楼层 |阅读模式
PHP复制代码
 
        public function getList($author){
                $sql="select ID,post_author,post_title,post_name from tt_posts where post_author='".$author."' or post_title LIKE '%".$author."%' order by ID desc limit 8";
                $result=$this->db->query($sql);
                return $result->result();
        }
 
复制代码


没有对$author进行处理。这样写存在sql注入风险吗?如果有风险,怎么防范。
发表于 2013-7-6 11:07:43 | 显示全部楼层
$author变量里面如果有一个';你的sql就被截断了
发表于 2013-7-6 12:17:24 | 显示全部楼层
PHP复制代码
$sql="select ID,post_author,post_title,post_name from tt_posts where post_author=".$this->db->escape($author)." or post_title LIKE '%".$this->db->escape_like_str($author)."%' order by ID desc limit 8";
$this->db->query($sql);
复制代码
 楼主| 发表于 2013-7-6 12:37:39 | 显示全部楼层
qi_ruo 发表于 2013-7-6 12:17

谢谢,很好很强大!
发表于 2013-7-6 20:05:05 | 显示全部楼层
手册里有这部分,可以详细参考
发表于 2013-7-8 17:04:14 | 显示全部楼层
請愛用CRUD!!

本版积分规则