Skip to content Skip to sidebar Skip to footer
Reading Time: < 1 minute

When you need to create duplicate/clone of mysql record in codeigniter this article will be a very useful. This can be used when you need to create a copy of high details of record. Just send 3 parameters to do this.

Put the below function in your model.

function DuplicateRecord($table, $primary_key_field, $primary_key_val) { 
    $this->db->where($primary_key_field, $primary_key_val); 
    $query = $this->db->get($table); 
    foreach ($query->result() as $row){ 
        foreach($row as $key=>$val) { 
            if($key != $primary_key_field) { 				
                //Below code can be used instead of passing a data array directly to the insert or update functions  
                $this->db->set($key, $val); 
            } //endif 
        } //endforeach 
    } //endforeach 
    //insert the new record into table 
    return $this->db->insert($table); 



$product_id = $this->input->post('id', TRUE); 
// remove white spaces 
if($product_id) { 
    $result = $this->Product_model->DuplicateRecord($table='product_details', $primary_key_field='product_id', $primary_key_val = $product_id); 
    if ($result) { 
        echo 'Copied Successfully'; 
    } else { 
        echo 'Failed to copy. Try again'; 


Use the below comment box to ask your problems and submit your ideas & suggestions.