$this->db->insert_or_update()

Tries to insert a row, if there will be a key violation will update the row instead

$this->db->insert_or_update('mytable', $insert_data, $update_data)

The first parameter is the name of the table that the insert/update will be operating on, the second parameter will be the data to attempt to insert, the last parameter is the data to use for updating the row.

$insert_data = array(
        'title' => 'My title',
        'name' => 'My Name',
        'create_date' => '2020-03-01'
        'created_by_id' => 5
        );
        
$update_data = array(
        'title' => 'My title',
        'update_date' => '2020-03-01'
        'updated_by_id' => 5
        );
        

$this->db->insert_or_update('mytable', $insert_data, $update_data);

  // Produces :
  // INSERT INTO mytable SET `title` = "My title", `name` = "My Name", `create_date` = "2020-03-01", `created_by_id` = 5 
      ON DUPLICATE KEY UPDATE `title` = "My title", `update_date` = "2020-03-01", `updated_by_id` = 5 
  

Added 2 April 2021