php - codeigniter : inserting data/record into 2 tables -
i have 2 tables 'pengguna' , 'mahasiswa' , 1 have form 1 form inserting 2 tables, far manage insert data when has "primary key" , "foreign key" has problem, u can see code below id_pengguna
table pengguna
primary key , id_pengguna
table mahasiswa
foreign key problem when inserting data, id_pengguna
pengguna
has value while in table mahasiswa
has no value, below code, there simple way or doing 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; }
you need return last insert id form 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 mahasiswa table ); $insert1 = $this->mmahasiswa->create('mahasiswa',$data);
Comments
Post a Comment