How would I use ON DUPLICATE KEY UPDATE in my CodeIgniter model?

前端 未结 8 1529
暖寄归人
暖寄归人 2020-11-30 07:27

I have a CodeIgniter/PHP Model and I want to insert some data into the database.

However, I have this set in my \'raw\' SQL query:

ON DUPLICATE KEY U         


        
8条回答
  •  北海茫月
    2020-11-30 08:31

    You can add the "ON DUPLICATE" statement without modifying any core files.

    $sql = $this->db->insert_string('table', $data) . ' ON DUPLICATE KEY UPDATE duplicate=LAST_INSERT_ID(duplicate)';
    $this->db->query($sql);
    $id = $this->db->insert_id();
    

    I hope it's gonna help someone.

提交回复
热议问题