简体   繁体   English

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

[英]How to insert data in 2 tables at a same time in codeigniter?

I have one form from where i am inserting data in one table.now i want to add my data in another table too how can i achieved this in code igniter.my second table is fin_tran Controller :我有一个表格,我在一个表中插入数据。现在我想将我的数据添加到另一个表中,我如何在代码点火器中实现这一点。我的第二个表是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');        
    }

model:模型:

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;
    }

I think you want to have a batch query that will add data on 2 tables otherwise none of them will have the data.我想你想要一个批处理查询,将在 2 个表上添加数据,否则它们都不会有数据。 If that so, Try to use如果是这样,请尝试使用

$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();
}

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

that will allow you to commit or rollback queries .这将允许您提交或回滚查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM