php - 防止 CodeIgniter 中的 SQL 注入(inject)

标签 php mysql codeigniter

CI Active Record 是否可以防止 SQL 注入(inject)?

这是我的代码

    $this->db->select('t.DateTime,
                       su.FirstName,
                       su.MiddleName,
                       su.LastName,
                       l.Description,
                       t.Temperature,
                       t.UnitID,
                       t.Humidity');
    $this->db->from('temperaturelogs as t');
    $this->db->join('systemusers as su', 'su.UserID = t.Encoder');
    $this->db->join('locations as l', 't.LocationID = l.LocationID');
    $this->db->where("Cast(t.DateTime as date) >= '$start_date'");
    $this->db->where("Cast(t.DateTime as date) <= '$end_date'");
    $query = $this->db->get();

    if ($query->num_rows() > 0) 
    {
        return $query->result_array();
    }

当我尝试在结束日期中输入此输入时。

我的输入: ';截断表系统用户; #

给我这个错误:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'truncate table systemusers; #'' at line 6

SELECT t.DateTime, su.FirstName, su.MiddleName, su.LastName, l.Description, t.Temperature, t.UnitID, t.Humidity FROM temperaturelogs as t JOIN systemusers as su ON su.UserID = t.Encoder JOIN locations as l ON t.LocationID = l.LocationID WHERE Cast(t.DateTime as date) >= '2016-03-21' AND Cast(t.DateTime as date) <= ''; truncate table systemusers; #'

该错误与我的问题没有任何关系...

最佳答案

尝试,

$this->db->where('Cast(t.DateTime as date) >=', $start_date);
$this->db->where('Cast(t.DateTime as date) <=', $end_date);

关于php - 防止 CodeIgniter 中的 SQL 注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36146583/

相关文章:

hash - 如何打开 hash_algos() ?我使用的是 Php 5.2.11

php - 将 javascript var 分配给 php return

Python 和 MySql - 无法获取几何对象

PHP mySQL 查询无法使用 SQL TIME 类型字段中的变量

javascript - 使用 jquery 的 CodeIgniter 动态输入并插入到数据库

CodeIgniter:遇到格式不正确的数值

php - 为什么在扩展 protected 类变量并将其标记为私有(private)后会得到 "Access Level must be protected or weaker"?

php - 不从 PHP 数据库查询返回结果集

sql - MySQL 带条件的左连接

php - MySQL/PHP 中的时区偏移量(和 st/dst 更改)