【发布时间】:2017-07-15 15:53:39
【问题描述】:
我知道 CodeIgniter 会自动转义正在发送的值以表示插入或更新查询,例如$bar,但是如果从说一个帖子或得到的表中收到表,它也会逃脱$table吗?我找不到任何相关文档。
$this->db->insert($table, array('foo' => $bar));
【问题讨论】:
标签: codeigniter escaping codeigniter-3
我知道 CodeIgniter 会自动转义正在发送的值以表示插入或更新查询,例如$bar,但是如果从说一个帖子或得到的表中收到表,它也会逃脱$table吗?我找不到任何相关文档。
$this->db->insert($table, array('foo' => $bar));
【问题讨论】:
标签: codeigniter escaping codeigniter-3
如果你查看 CodeIgniter 的 2.x system/database/drivers/DB_driver.php 靠近 902 行
或
在 CodeIgniters 3.x 系统/数据库/DB_driver 1365 行附近
您会发现一个名为 insert_string() 的函数,如下所示:
/**
* Generate an insert string
*
* @access public
* @param string the table upon which the query will be performed
* @param array an associative array data of key/values
* @return string
*/
function insert_string($table, $data)
{
$fields = array();
$values = array();
foreach ($data as $key => $val)
{
$fields[] = $this->_escape_identifiers($key);
$values[] = $this->escape($val);
}
return $this->_insert($this->_protect_identifiers($table, TRUE, NULL, FALSE), $fields, $values);
}
然后在第 1246 行(CI 2.x)或第 1729 行(CI 3.0)附近的后续函数 _protect_identifiers() 表示:
* Since the column name can include up to four segments (host, DB, table, column)
* or also have an alias prefix, we need to do a bit of work to figure this out and
* insert the table prefix (if it exists) in the proper position, and escape only
* the correct identifiers.
所以答案是肯定的。
如有疑问,您可以随时使用:echo ($this->db->last_query());die();,它会打印出您执行的最后一个查询,如下所示:
INSERT INTO `googlemaps_marker` (`descr`, `Lat`, `Lng`, `pretty_url`, `ID`, `zone_ID`, `kind`, `author_id`, `author`, `date_updated`) VALUES ('sasasasdas', '41.27780646738183', '-7.437744140625', 'sasasasdas', 4, 4, 1, '1', 'Admini Istrator', '2017-07-15 18:20:40')
【讨论】: