如何在codeigniter中同时在2个表中插入数据?

时间:2018-02-07 16:09:10

标签: php codeigniter

我有一个表单从我在一个表中插入数据。现在我想在另一个表中添加我的数据如何在代码igniter.my第二个表fin_tran中实现这一点 控制器:

public function insert_uk_slip()
    {               

        $employee_name = $this->input->post('employee_name');     
        $net_pay = $this->input->post('net_pay');
        $userInfo = array('emp_id'=>$employee_name,           
            'net_pay'=>$net_pay,  
            );            
        $this->load->model('Pay_slips_model');
        $result = $this->Pay_slips_model->insert_uk_slip($userInfo);            
        if($result > 0)
        {
            $this->session->set_flashdata('success', 'New User created successfully');
        }
        else
        {
            $this->session->set_flashdata('error', 'User creation failed');
        }            
        redirect('responsible/upload_uk_slip');        
    }

模型:

public function insert_uk_slip($userInfo)
    {
        $this->db->trans_start();
        $this->db->insert('uk_new_salary_slip', $userInfo);            
        $insert_id = $this->db->insert_id();            
        $this->db->trans_complete();            
        return $insert_id;
    }

1 个答案:

答案 0 :(得分:0)

我想你想要一个批量查询,它将在2个表上添加数据,否则它们都不会有数据。如果是这样,请尝试使用

$this->db->trans_begin();

$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');

if ($this->db->trans_status() === FALSE)
{
        $this->db->trans_rollback();
}
else
{
        $this->db->trans_commit();
}

请阅读: https://www.codeigniter.com/user_guide/database/transactions.html

允许您提交或回滚查询。