Php 如何将多个数据集插入多个表

Php 如何将多个数据集插入多个表,php,codeigniter,Php,Codeigniter,我是Codeigniter的初学者。我目前正在创建一个注册页面。我想使用CodeIgniter一次将数据输入4个表中。该表为tbl_asesi、tbl_pdd_非正规、tbl_工作、tbl_组织,但新数据出现在一个表中,即tbl_asesi,而其他表仍然为空。你能帮我修一下密码吗 我使用的控制器如下 我的控制器: <?PHP defined('BASEPATH') OR exit('No direct script access allowed'); class Pendaftaran

我是Codeigniter的初学者。我目前正在创建一个注册页面。我想使用CodeIgniter一次将数据输入4个表中。该表为tbl_asesi、tbl_pdd_非正规、tbl_工作、tbl_组织,但新数据出现在一个表中,即tbl_asesi,而其他表仍然为空。你能帮我修一下密码吗

我使用的控制器如下

我的控制器:

<?PHP
defined('BASEPATH') OR exit('No direct script access allowed');

class Pendaftaran extends CI_Controller {

  public function index()
  {

    $data = array(

      'bidang' => $this->m_pendaftaran->get_bidang(),
      'subbidang' => $this->m_pendaftaran->get_subbidang(),
      'jenis_bidang' => $this->m_pendaftaran->get_jenis_bidang(),
      'level' => $this->m_pendaftaran->get_level(),
      'okupasi' => $this->m_pendaftaran->get_okupasi(),
      'unitinti' => $this->m_pendaftaran->get_unitinti(),
      'unitpilihan1' => $this->m_pendaftaran->get_unitpilihan1(),
      'unitpilihan2' => $this->m_pendaftaran->get_unitpilihan2(),
      'unitpilihan3' => $this->m_pendaftaran->get_unitpilihan3(),
      'bidang_selected' => '',
      'subbidang_selected' => '',
      'jenis_bidang_selected' => '',
      'level_selected' => '',
      'okupasi_selected' => '',
      'unitinti_selected' => '',
      'unitpilihan1_selected' => '',
      'unitpilihan2_selected' => '',
      'unitpilihan3_selected' => '',
    );

    $this->template->load('static','pendaftaran',$data);

  }

  function __construct(){
   parent::__construct();
       $this->load->library(array('form_validation'));
       $this->load->model('m_pendaftaran');
       $this->load->helper(array('form', 'url'));

     }

     public function daftarasesi() {

      $this->form_validation->set_rules('nama', 'NAMA','required'); 
      $this->form_validation->set_rules('nik', 'NIK','required'); 
      $this->form_validation->set_rules('jenis_kelamin', 'JENIS_KELAMIN','required'); 
      $this->form_validation->set_rules('alamat', 'ALAMAT','required');   
      $this->form_validation->set_rules('jabatan', 'JABATAN','');      
      $this->form_validation->set_rules('asal_institusi', 'ASAL_INSTITUSI','required');
      $this->form_validation->set_rules('alamat_institusi', 'ALAMAT_INSTITUSI','required');
      $this->form_validation->set_rules('no_hp', 'NO_HP','required');  
      $this->form_validation->set_rules('email','EMAIL','required|valid_email');
      $this->form_validation->set_rules('sd', 'SD','required');
      $this->form_validation->set_rules('kota_sd', 'KOTA_SD','required');
      $this->form_validation->set_rules('tahun_sd', 'TAHUN_SD','required');
      $this->form_validation->set_rules('smp', 'SMP','required');
      $this->form_validation->set_rules('kota_smp', 'KOTA_SMP','required');
      $this->form_validation->set_rules('tahun_smp', 'TAHUN_SMP','required');
      $this->form_validation->set_rules('sma', 'SMA','required');    
      $this->form_validation->set_rules('kota_sma', 'KOTA_SMA','required'); 
      $this->form_validation->set_rules('tahun_sma', 'TAHUN_SMA','required');
      $this->form_validation->set_rules('d', 'D');    
      $this->form_validation->set_rules('kota_d', 'KOTA_D'); 
      $this->form_validation->set_rules('tahun_d', 'TAHUN_D');
      $this->form_validation->set_rules('s1', 'S1');    
      $this->form_validation->set_rules('kota_s1', 'KOTA_S1'); 
      $this->form_validation->set_rules('tahun_s1', 'TAHUN_S1');  
      $this->form_validation->set_rules('s2', 'S2');    
      $this->form_validation->set_rules('kota_s2', 'KOTA_S2'); 
      $this->form_validation->set_rules('tahun_s2', 'TAHUN_S2');     
      $this->form_validation->set_rules('s3', 'S3');    
      $this->form_validation->set_rules('kota_s3', 'KOTA_S3'); 
      $this->form_validation->set_rules('tahun_s3', 'TAHUN_S3'); 
      $this->form_validation->set_rules('pelatihan', 'PELATIHAN');
      $this->form_validation->set_rules('lembaga', 'LEMBAGA');
      $this->form_validation->set_rules('kota_pelatihan', 'KOTA_PELATIHAN');   
      $this->form_validation->set_rules('tahun_pelatihan', 'TAHUN_PELATIHAN'); 
      $this->form_validation->set_rules('kegiatan', 'KEGIATAN'); 
      $this->form_validation->set_rules('institusi', 'INSTITUSI'); 
      $this->form_validation->set_rules('tahun_kerja', 'TAHUN_KERJA'); 
      $this->form_validation->set_rules('nama_organisasi', 'NAMA ORGANISASI');
      $this->form_validation->set_rules('status_anggota', 'STATUS ANGGOTA');
      $this->form_validation->set_rules('surat_organisasi', 'SURAT ORGANISASI');
      $this->form_validation->set_rules('tahun_organisasi', 'TAHUN_ORGANISASI');
      $this->form_validation->set_rules('bidang', 'BIDANG','required');
      $this->form_validation->set_rules('subbidang', 'SUBBIDANG','required');
      $this->form_validation->set_rules('level', 'LEVEL','required');
      $this->form_validation->set_rules('jenis_bidang', 'JENIS_BIDANG','required');
      $this->form_validation->set_rules('okupasi', 'OKUPASI','required');
      $this->form_validation->set_rules('unitinti', 'UNITINTI','required');
      $this->form_validation->set_rules('unitpilihan1', 'UNITPILIHAN1','required');
      $this->form_validation->set_rules('unitpilihan2', 'UNITPILIHAN2','');
      $this->form_validation->set_rules('unitpilihan3', 'UNITPILIHAN3',''); 

      if($this->form_validation->run() == FALSE) {
        $this->session->set_flashdata('gagaldaftar', 'Maaf, Pendaftaran Gagal Dilakukan.');
        $this->template->load('static','pendaftaran');
      }else{

        $data_asesi['nama']             =    $this->input->post('nama');  
        $data_asesi['nik']              =    $this->input->post('nik');   
        $data_asesi['jenis_kelamin']    =    $this->input->post('jenis_kelamin');  
        $data_asesi['alamat']           =    $this->input->post('alamat');  
        $data_asesi['jabatan']          =    $this->input->post('jabatan');
        $data_asesi['asal_institusi']   =    $this->input->post('asal_institusi');
        $data_asesi['alamat_institusi'] =    $this->input->post('alamat_institusi');
        $data_asesi['no_hp']            =    $this->input->post('no_hp');           
        $data_asesi['email']            =    $this->input->post('email');
        $data_asesi['sd']               =    $this->input->post('sd');
        $data_asesi['kota_sd']          =    $this->input->post('kota_sd');
        $data_asesi['tahun_sd']         =    $this->input->post('tahun_sd');
        $data_asesi['smp']              =    $this->input->post('smp');
        $data_asesi['kota_smp']         =    $this->input->post('kota_smp');
        $data_asesi['tahun_smp']        =    $this->input->post('tahun_smp');
        $data_asesi['sma']              =    $this->input->post('sma'); 
        $data_asesi['kota_sma']         =    $this->input->post('kota_sma');
        $data_asesi['tahun_sma']        =    $this->input->post('tahun_sma'); 
        $data_asesi['d']                =    $this->input->post('d'); 
        $data_asesi['kota_d']           =    $this->input->post('kota_d');
        $data_asesi['tahun_d']          =    $this->input->post('tahun_d');      
        $data_asesi['s1']               =    $this->input->post('s1'); 
        $data_asesi['kota_s1']          =    $this->input->post('kota_s1');
        $data_asesi['tahun_s1']         =    $this->input->post('tahun_s1'); 
        $data_asesi['s2']               =    $this->input->post('s2'); 
        $data_asesi['kota_s2']          =    $this->input->post('kota_s2');
        $data_asesi['tahun_s2']         =    $this->input->post('tahun_s2'); 
        $data_asesi['s3']               =    $this->input->post('s3'); 
        $data_asesi['kota_s3']          =    $this->input->post('kota_s3');
        $data_asesi['tahun_s3']         =    $this->input->post('tahun_s3'); 

        //tabel tbl_pdd_non_formal
        $data_non_formal = array();
        $data_non_formal['pelatihan']        =    $this->input->post('pelatihan');
        $data_non_formal['lembaga']          =    $this->input->post('lembaga');
        $data_non_formal['kota_pelatihan']   =    $this->input->post('kota_pelatihan');
        $data_non_formal['tahun_pelatihan']  =    $this->input->post('tahun_pelatihan');

        //tabel tbl_pekerjaan
        $data_pekerjaan = array();
        $data_pekerjaan['kegiatan']         =    $this->input->post('kegiatan');
        $data_pekerjaan['institusi']        =    $this->input->post('institusi');
        $data_pekerjaan['surat_pendukung']  =    $this->input->post('surat_pendukung');
        $data_pekerjaan['tahun_kerja']      =    $this->input->post('tahun_kerja');

        //tbl_organisasi
        $data_organisasi = array();
        $data_organisasi['nama_organisasi']  =    $this->input->post('nama_organisasi');
        $data_organisasi['status_anggota']   =    $this->input->post('status_anggota');
        $data_organisasi['surat_organisasi'] =    $this->input->post('surat_organisasi');
        $data_organisasi['tahun_organisasi'] =    $this->input->post('tahun_organisasi');

        //tbl_asesi
        $data_asesi['bidang']           =    $this->input->post('bidang'); 
        $data_asesi['subbidang']        =    $this->input->post('subbidang'); 
        $data_asesi['level']            =    $this->input->post('level');
        $data_asesi['jenis_bidang']     =    $this->input->post('jenis_bidang');  
        $data_asesi['okupasi']          =    $this->input->post('okupasi'); 
        $data_asesi['unitinti']         =    $this->input->post('unitinti'); 
        $data_asesi['unitpilihan1']     =    $this->input->post('unitpilihan1'); 
        $data_asesi['unitpilihan2']     =    $this->input->post('unitpilihan2'); 
        $data_asesi['unitpilihan3']     =    $this->input->post('unitpilihan3');

        // var_dump($data_asesi);
        // var_dump($data_non_formal);
        // var_dump($data_pekerjaan);
        // var_dump($data_organisasi);

        $this->m_pendaftaran->daftarasesi($data_asesi, $data_non_formal, $data_pekerjaan, $data_organisasi);

        // $this->m_pendaftaran->daftarasesi($data); 

        $this->session->set_flashdata('berhasil', 'Pendaftaran Berhasil Dilakukan.');         
        $this->template->load('static','pendaftaran');
      }
    }          

    public function lakukan_download_asesi(){       
    force_download('berkas/berkas_asesi.rar',NULL);
   }

 }

我认为您做错了,在Codeigniter中没有名为
$this->db->insert_nik()
的查询生成器函数,而您正在返回此函数,因此它无法工作。所以你不应该用这个

您可以按以下方式从一个表单向多个表中插入数据,并在模型中使用此方法

public function daftarasesi($asesi, $non_formal, $pekerjaan, $organisasi)
{ 
    $this->db->trans_start();

    // insert data asesi //
    $this->db->insert('tbl_asesi',$asesi);

    // insert data non formal //
    $this->db->insert('tbl_pdd_non_formal',$non_formal);

    // insert data pekerjaan //
    $this->db->insert('tbl_pekerjaan',$pekerjaan);

    // insert data pekerjaan //
    $this->db->insert('tbl_pekerjaan',$pekerjaan);

    // insert data organisasi //
    $this->db->insert('tbl_organisasi',$organisasi);

    $this->db->trans_complete();

    if ($this->db->trans_status() === FALSE)
    {
        $this->db->trans_rollback();
        return FALSE;

    } else {

        $this->db->trans_commit();
        return TRUE;
    }

}
您可以在开始/完成之间运行任意数量的查询 函数,它们都将根据 任何给定查询的成功或失败

注意:处理多个插入/更新时,始终使用事务


我认为您做错了,在Codeigniter中没有名为
$this->db->insert_nik()
的查询生成器函数,而您返回了该函数,因此它不起作用。所以你不应该用这个

您可以按以下方式从一个表单向多个表中插入数据,并在模型中使用此方法

public function daftarasesi($asesi, $non_formal, $pekerjaan, $organisasi)
{ 
    $this->db->trans_start();

    // insert data asesi //
    $this->db->insert('tbl_asesi',$asesi);

    // insert data non formal //
    $this->db->insert('tbl_pdd_non_formal',$non_formal);

    // insert data pekerjaan //
    $this->db->insert('tbl_pekerjaan',$pekerjaan);

    // insert data pekerjaan //
    $this->db->insert('tbl_pekerjaan',$pekerjaan);

    // insert data organisasi //
    $this->db->insert('tbl_organisasi',$organisasi);

    $this->db->trans_complete();

    if ($this->db->trans_status() === FALSE)
    {
        $this->db->trans_rollback();
        return FALSE;

    } else {

        $this->db->trans_commit();
        return TRUE;
    }

}
您可以在开始/完成之间运行任意数量的查询 函数,它们都将根据 任何给定查询的成功或失败

注意:处理多个插入/更新时,始终使用事务


我想你必须更具体一点,但我看到的是:
$this->db->insert_nik()
不是Codeigniter中的已知函数,您需要删除模型函数
daftarasesi
中的前两个
return
语句,并且为了避免更新/插入问题,您必须从html表单中发送相应的id。我将使用主键在nik列中输入数据。因此,我使用$this->db->insert_nik()。有没有其他解决方案来解决这个错误?我想你必须更具体一点,但我看到的是:
$this->db->insert_nik()
不是Codeigniter中的已知函数,您需要删除模型函数
daftarasesi
中的前两个
return
语句,并且为了避免更新/插入问题,您必须从html表单中发送相应的id。我将使用主键在nik列中输入数据。因此,我使用$this->db->insert_nik()。是否有其他解决方案来解决此错误?我尝试过这样做,但结果仍然是一个错误。错误在于插入非形式数据时的模型。还有其他解决方案吗?我尝试过这样做,但结果仍然是错误的。错误在于插入非形式数据时的模型。还有其他解决方案吗?