如何将多个数据集插入到多个表中



我是Codeigniter的初学者。我目前正在创建一个注册页面。我想使用 CodeIgniter 一次将数据输入到 4 个表中。该表是tbl_asesi、tbl_pdd_non_formal、tbl_work、tbl_organization,但新数据来自 1 个表,即 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);
}
}

我使用的模型如下

我的模型 :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class M_pendaftaran extends CI_Model 
{
public function __construct()
{
parent::__construct();
}
public function daftarasesi($asesi, $non_formal, $pekerjaan, $organisasi)
{ 
// insert data asesi //
$this->db->insert('tbl_asesi',$asesi);
$nik_fk = $this->db->insert_nik();
// insert data non formal //
$data_non_formal['nik_fk'] = $nik_fk;
$this->db->insert('tbl_pdd_non_formal',$non_formal);
return $insert_nik = $this->db->insert_nik();
// insert data pekerjaan //
$data_pekerjaan['nik_fk'] = $nik_fk;
$this->db->insert('tbl_pekerjaan',$pekerjaan);
return $insert_nik = $this->db->insert_nik();
// insert data organisasi //
$data_organisasi['nik_fk'] = $nik_fk;
$this->db->insert('tbl_organisasi',$organisasi);
return $insert_nik = $this->db->insert_nik();
}

我认为你做错了,在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;
}
}

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

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

相关内容

  • 没有找到相关文章

最新更新