我想将一些值添加到多个表中,但现在我的代码不是那么好。我已经注释掉了想要添加到哪些表中的值。
名为 Add_employee 的控制器
public function addemp()
{
//id like to add this to 'employees'
$data1 = array(
'emp_no' => $this->input->post('emp_no'),
'birth_date' => $this->input->post('birth_date'),
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'gender' => $this->input->post('gender'),
'hire_date' => $this->input->post('hire_date'),
);
//idd like to add this to 'titles'
$data2 = array(
'emp_no' => $this->input->post('emp_no'),
'title' => $this->input->post('title')
);
//idd like to add this to 'dept_emp' and id like to add value 'emp_no' to the table 'salaries'
$data3 = array(
'emp_no' => $this->input->post('emp_no'),
'dept' => $this->input->post('dept')
);
$mdata ['m1'] = $data1;
$mdata ['m2'] = $data2;
$mdata ['m3'] = $data3;
$this->add_model->adddata($mdata);
$this->load->view('add_view', $data1);
}
名为 Add_model 的模型
public function adddata($data) {
//extract();
$this->db->insert('employees', $m1);
$this->db->insert('titles', $m2);
$this->db->insert('dept_emp', $m3);
//$this->db->insert('salaries', $emp_no);//$emp_no => 'emp_no');
return;
}