How to insert one id into multi table with Codeigniter.

 

My SQL Database

Table Structure of tb_employee
            employee_id
            name
            phone
           ...........

Table Structure of tb_permission
         employee_id
         sales
         customers
         ................

Table Structure of tb_user
         employee_id
         user_name
         password
         ...............

Now let start coding:

Controler:

    public function add_employee()
    {
        $data['employees_data']=$this->employee_model->insert_employee();
        $latest_id=$this->employee_model->get_latest_id();
        $data['permissions_data']=$this->employee_model->insert_permission($latest_id);
        $data['users_data']=$this->employee_model->insert_user($latest_id);
   
        redirect('employees');
    }   

Model:

    function insert_employee()
    {
        //set preferences
        $config['upload_path'] = './uploads/users/';
        $config['allowed_types'] = '*';
        $config['max_size'] = '10000000';
       
        //load library of file upload
        $this->load->library('upload', $config);
       
        //copy file into folder
        if(!$this->upload->do_upload('files'))
        {
            $file = "";
           
        }else{
           
            $upload_data = $this->upload->data();
            $file = $upload_data['file_name'];
           
        }
       
        $data=array(
            'name' => $this->input->post('name'),
            'phone' => $this->input->post('phone'),
            'email' => $this->input->post('email'),
            'address' => $this->input->post('address'),
            'files' =>  $file,
            'message' => $this->input->post('message')
        );
        $this->db->insert('tb_employee',$data);
        //better return true on success
    }

    public function get_latest_id()
    {
        $sql=$this->db->query("SELECT MAX(employee_id) as id FROM tb_employee");
        return $sql->row_array();
    }
   
    function insert_permission($emp_id)
    {
        $data=array(
            'employee_id' => $emp_id['id'],
            'items' => $this->input->post('items'),
            'sales' => $this->input->post('sales'),
            'employees' =>   $this->input->post('employees'),
            'customers' =>   $this->input->post('customers'),
            'suppliers' =>   $this->input->post('suppliers'),
            'receivings' =>   $this->input->post('receivings'),
            'reports' =>   $this->input->post('reports'),
            'config' =>   $this->input->post('configuration')
        );
        $this->db->insert('tb_permissions',$data);
    }

  function insert_user($emp_id)
    {
        $data=array(
            'employee_id' => $emp_id['id'],
            'user_name' => $this->input->post('user_name'),
            'password' => $this->input->post('password')
        );
        $this->db->insert('tb_users',$data);
    }

Share this

Related Posts

First

Newest questions tagged codeigniter - Stack Overflow