Diasline
Diasline

Reputation: 635

How to copy data from one table to another “EXCEPT” one field Codeigniter

How can I use codeigniter to copy all data of table1 to table2 except the primary key of table1. table1 and table2 have the same structure.

I try this :

$query = $this->db->get_where('table1',array('patient_id'=>$this->input->post('patient_id')));
foreach ($query->result() as $row) {
$this->db->insert('table2',$row);
}

It works but the primary key of table1 is inserted as well.

How can I ignore the primary key on table1 ?

Thanks in advance

Upvotes: 0

Views: 234

Answers (1)

Cue
Cue

Reputation: 2759

Assuming patient_id is the primary key in question, you can remove the data from the result object with unset.

$query = $this->db->get_where('table1',array('patient_id'=>$this->input->post('patient_id')));
foreach ($query->result() as $row) {
    unset($row->patient_id);
    $this->db->insert('table2',$row);
}

Upvotes: 1

Related Questions