首页 > 解决方案 > 多插入行和表codeigniter

问题描述

我有 1 个新客户,它将插入 2 个表('tbcust' & 'tbcp')。我为表'tcp'中的插入行添加按钮'添加新联系人',按钮保存在表单中将插入多个表。表 'tbcust' 没问题,但表 'tbcp' 中的行 'nama_pt' 只传递了 1 个字符。

控制器

public function post_multiple_table(){

        $this->load->model('Multiple_model', 'multi_model', TRUE);
        $cust_input_data = array();
        //$cp_input_data = array();

        $cust_input_data['nama_pt'] = $this->input->post('nama_pt');
        $cust_input_data['tipe'] = $this->input->post('tipe');
        $cust_input_data['alamat'] = $this->input->post('alamat');
        $cust_input_data['email_cust'] = $this->input->post('email_cust');
        $cust_input_data['notelp'] = $this->input->post('notelp');
        $cust_input_data['nofax'] = $this->input->post('nofax');

        $this->form_validation->set_rules('nama_orang[]', 'nama_orang', 'required|trim|xss_clean');
        $this->form_validation->set_rules('nama_pt', 'nama_orang', 'required|trim|xss_clean');
        $this->form_validation->set_rules('nohp[]', 'nohp', 'required|trim|xss_clean');
        $this->form_validation->set_rules('email[]', 'email', 'required|trim|xss_clean');

        if ($this->form_validation->run() == FALSE){
         echo validation_errors(); // tampilkan apabila ada error
        }else{

        $nm = $this->input->post('nama_orang');
        $result = array();
        foreach($nm AS $key => $val){
        $result[] = array(
        "nama_orang"  => $_POST['nama_orang'][$key],
        "nama_pt"  => $_POST['nama_pt'][$key],
        "nohp"  => $_POST['nohp'][$key],
        "email"  => $_POST['email'][$key]
         );
        }

模型

public function create_multiple_tabel($nama_pt, $nama_orang){
        $this->db->insert('tbcust', $nama_pt);
        $this->db->insert_batch('tbcp', $nama_orang);
    }

这是表格和数据库 tbcp tbcust 形成新客户

标签: phpdatabasecodeigniter

解决方案


tbcp如果您的行nama_pt设置了长度,请检查表,如果需要,请1放置15或更高。


推荐阅读