简体   繁体   中英

SQL NOW() gives 0000-00-00 00:00:00

i have done SQL insertion with NOW() using codeigniter. instead of current time i got 0000-00-00 00:00:00 in my db recode. i am using latest WAMP as my server and cannot understand the where is the problem. please help me.

function createform($form_data)
{
    $this->db->set('created', 'NOW()',TRUE);
    $this->db->insert('amfbases', $form_data);

    if ($this->db->affected_rows() == '1')
    {
        return TRUE;
    }

    return FALSE;
}

check your column data type, and did u quote the NOW(), like insert into table values ("NOW()"); ?

updated

$this->db->set('created', time(),TRUE);

or

$this->db->set('created', date('Y-m-d H:i:s'),TRUE);

or update your table schema to add_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP

updated 2

NOW() is specify to MYSQL, PHP interpreted it as string, and cannot mix both together

Actually the right way to do this (ie pass sql functions to db) is to specify third param to be FALSE Example:

$this->db->set('created', 'NOW()', FALSE); // notice FALSE

That will cause codeigniter to pass second param without quoting it resulting in sql interpreting it as function.

Useful for:

$this->db->set('counter', 'counter + 1', FALSE);

For reference look: http://codeigniter.com/user_guide/database/active_record.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM