Codeigniter如何存储实时数据

时间:2018-11-22 12:25:30

标签: codeigniter

public function insert_employee($fldCompanyStringID) {

    $fldCompanyID = getCoompanyByStringID($fldCompanyStringID)->fldCompanyID;
    $data = array(
        'fldUserFName' => $this->input->post('fldUserFName'),
        'fldUserBankAccountNumber' => $this->input->post('fldUserBankAccountNumber')
    );
    $data2 = array(
        'fldWorkHistoryCompanyName' => $this->input->post('fldWorkHistoryCompanyName')
    );
    if ($this->db->insert('tblUser', $data)&& $this->db->insert(' tblWorkHistory', $data2)) {
        $this->session->set_flashdata('success_msg', 'New Employee is inserted');
    }
}

tblUser表自动生成一个userID。我想拿那个userID并将其存储到tblWorkHistory表**

2 个答案:

答案 0 :(得分:1)

此处CodeIgniter交易会为您提供帮助。

https://www.codeigniter.com/user_guide/database/transactions.html

请使用此代码-

<?php 

public function insert_employee($fldCompanyStringID) {

    $this->db->trans_start();

    $fldCompanyID = getCoompanyByStringID($fldCompanyStringID)->fldCompanyID;

    /* Insert User */
    $data = array(
        'fldUserFName' => $this->input->post('fldUserFName'),
        'fldUserBankAccountNumber' => $this->input->post('fldUserBankAccountNumber')
    );
    $this->db->insert('tblUser', $data)
    $insert_id = $this->db->insert_id();

    /* Insert Work History */
    $data2 = array(
        'userID' => $insert_id,
        'fldWorkHistoryCompanyName' => $this->input->post('fldWorkHistoryCompanyName')
    );
    $this->db->insert('tblWorkHistory', $data2)

    /* Manage Transaction */
    $this->db->trans_complete();
    if ($this->db->trans_status() === FALSE){
        $this->session->set_flashdata('error_msg', 'Failed, please try again');
    }else{
        $this->session->set_flashdata('success_msg', 'New Employee is inserted');
    }

}

?>

答案 1 :(得分:0)

$this->db->insert_id() is used to get the last insert auto increment id data.

$this->db->insert('tblUser', $data);
$insert_id = $this->db->insert_id();

if($insert_id) {
    $data2 = array(
        'userID' => $insert_id,
        'fldWorkHistoryCompanyName' => $this->input->post('fldWorkHistoryCompanyName')
    );

    if ($this->db->insert(' tblWorkHistory', $data2)) {
        $this->session->set_flashdata('success_msg', 'New Employee is inserted');
    }   
} else {
    $this->session->set_flashdata('error_msg', 'Something went wrong');
}
相关问题