codeigniter : inserting data/record into 2 tables

筅森魡賤 提交于 2020-01-02 11:03:08

问题


i have 2 tables 'pengguna' and 'mahasiswa' and then 1 have a form which is 1 form inserting into 2 tables, so far i manage to insert the data but when it has to do with "primary key" and "foreign key" it has problem, as u can see from code below id_pengguna from table pengguna is a primary key and id_pengguna from table mahasiswa is a foreign key the problem is when i inserting the data, id_pengguna from pengguna has it value while in table mahasiswa it has no value, below are my code, is there any simple way or am i doing something wrong?

PENGGUNA TABLE MAHASISWA TABLE

Controller

public function insertMahasiswa(){
    $username = $this->input->post('username');
    $password = md5($this->input->post('password'));
    $data1 = array(
            'username'=>$username,
            'password'=>$password,
            'level'=>3,
        );
    $nim = $this->input->post('nim');
    $nama = $this->input->post('nama');
    $email = $this->input->post('email');
    $telepon = $this->input->post('telepon');
    $data = array(
            'nim'=>$nim,
            'nama_mahasiswa'=>$nama,
            'email'=>$email,
            'telepon'=>$telepon,
        );
    $insert = $this->MMahasiswa->create('pengguna',$data1);
    $insert1 = $this->MMahasiswa->create('mahasiswa',$data);
    redirect(site_url('mahasiswa/data?balasan=1'));
}

MODEL

function create($table,$data){
    $query = $this->db->insert($table, $data);
    return $query;
}

回答1:


You need to return last insert id form your model file

In model

function create($table,$data){
    $query = $this->db->insert($table, $data);
    return $this->db->insert_id();// return last insert id
}

In controller

$id_pengguna = $this->MMahasiswa->create('pengguna',$data1);
    $data = array(
                'nim'=>$nim,
                'nama_mahasiswa'=>$nama,
                'email'=>$email,
                'telepon'=>$telepon,
                'id_pengguna'=>$id_pengguna// add last insert id to mahasiswa table
            );
        $insert1 = $this->MMahasiswa->create('mahasiswa',$data);



回答2:


Before inserting data to the 2nd table i.e. mahasiswa you need to grab the primary id created after insertion of data in table 1 (pengguna).

This can be done in 2 ways:

  1. Grabbing last insert id using codeigniter's insert_id() function.

    function create($table,$data) {
        $query = $this->db->insert($table, $data);
        return $this->db->insert_id(); } // return last insert id
    
  2. After inserting the data in 1st table, get the id of the row which has the same username. (For this you must have unique usernames in username column) This will return the corresponding id.
    Note: You can also do this with email column.

Source/Reference: Detailed explanation - Inserting data in 2 tables through 1 Form - CodeIgniter




回答3:


you can do this (only few changes)

controller.php

public function insertMahasiswa(){
    $username = $this->input->post('username');
    $password = md5($this->input->post('password'));

    $data1 = array(
            'username'=>$username,
            'password'=>$password,
            'level'=>3,
        );

    $nim = $this->input->post('nim');
    $nama = $this->input->post('nama');
    $email = $this->input->post('email');
    $telepon = $this->input->post('telepon');

    $data = array(
            'nim'=>$nim,
            'nama_mahasiswa'=>$nama,
            'email'=>$email,
            'telepon'=>$telepon,
        );
    $insert = $this->MMahasiswa->create('pengguna',$data1);
    $insert1 = $this->MMahasiswa->create('mahasiswa',$data);

    $insert = $this->MMahasiswa->create('pengguna',$data1,'mahasiswa',$data);

    redirect(site_url('mahasiswa/data?balasan=1'));
}

model.php

function create($table1, $data1, $table2, $data2){
    $this->db->insert($table1, $data1);
    $id_table1    = $this->db->insert_id();

    array_unshift($data2, array('id_pengguna'=>$id_table1));

    $this->db->insert($table2, $data2);
    $id_table2    = $this->db->insert_id();

    $return_data  = array($table1 => $id_table1, $table2 => $id_table2);

    return $returndata;
}


来源:https://stackoverflow.com/questions/36709801/codeigniter-inserting-data-record-into-2-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!