Mirza Chilman
Mirza Chilman

Reputation: 429

codeigniter : inserting data/record into 2 tables

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 enter image description here MAHASISWA TABLE enter image description here

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;
}

Upvotes: 3

Views: 23552

Answers (3)

Aman Dhanda
Aman Dhanda

Reputation: 438

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

Upvotes: 1

elddenmedio
elddenmedio

Reputation: 1030

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;
}

Upvotes: 0

Saty
Saty

Reputation: 22532

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);

Upvotes: 4

Related Questions