[CodeIgniter] MySQL NOW()

Was trying to do insert/update using CodeIgniter Active Record helpers, and realize the usual way of inserting doesn't work, because the helper functions actually escaped the values to prevent SQL injection.

So we can't do the just group all the data together the easy way.
$data['field1'] = $data1;
$data['field1'] = $data2
$data['created_date'] = 'NOW()';
$this -> db -> insert($this -> tables['some_table'], $data);

There are two possible ways to go around this.

1. Use CodeIgniter $db -> set() function, with the additional FALSE parameter to prevent data from being escaped.
$this -> db -> set('created_date', 'NOW()', FALSE);
$this -> db -> insert($this -> tables['some_table'], $data);

2. Use PHP data function to generate datetime string. The only thing to note with this method is, in most cases, the PHP server time and the MySQL time is different.
$data['created_date'] = date('Y-m-d H:i:s', time();
$this -> db -> insert($this -> tables['some_table'], $data);



Popular posts from this blog


Generate GoDaddy SSL Certificate (.crt) for Azure Websites (.pfx)

PayDollar Test Credit Cards