I tried to insert data into multiple tables that have foreign key, using CodeIgniter.
here's my first table called koor_pen
no_koor (primary) | utm_y | utm_x | latit | longi
here's my second table called input_pen
no_form (primary) | kode_bps | no_obs | no_koor (foreign) | t_tanah | catatan
here's my controller
function c_submit(){
$data = array(
'no_form' => $this->input->post('noform'),
'kode_bps' => $this->input->post('kodebps'),
'no_obs' => $this->input->post('noobs'),
'no_koor' => $this->input->post('nokoor'),
'tanaman_u' => $this->input->post('tutama'),
't_tanah' => $this->input->post('ttanah'),
'catatan' => $this->input->post('cat')
);
$datakoor = array(
'no_koor' => $this->input->post('nokoor'),
'utm_y' => $this->input->post('y'),
'utm_x' => $this->input->post('x'),
'latit' => $this->input->post('deg')." ".
$this->input->post('min')." ".
$this->input->post('sec'),
'longi' => $this->input->post('deg2')." ".
$this->input->post('min2')." ".
$this->input->post('sec2')
);
$no_obs = $this->session->userdata('no_obs');
$this->m_input->m_submit($data, $datakoor);
redirect(base_url("c_input"));
}
and the model
function m_submit($data, $datakoor) {
$this->db->trans_start();
$this->db->insert('koor_pen', $datakoor);
$no_koor = $this->db->insert_id();
$this->db->where('no_koor',$no_koor);
$this->db->insert('input_pen', $data);
$this->db->trans_complete();
return $this->db->insert_id();
}
