title: codeigniter : inserting data/record into 2 tables
body: <p>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 <code>id_pengguna</code> from table <code>pengguna</code> is a <strong>primary key</strong> and <code>id_pengguna</code> from table <code>mahasiswa</code> is a <strong>foreign key</strong> the problem is when i inserting the data, <code>id_pengguna</code> from <code>pengguna</code> has it value while in table <code>mahasiswa</code> it has no value, below are my code, is there any simple way or am i doing something wrong?</p>
  
  <p><strong>PENGGUNA TABLE</strong>
  <a href="https://i.stack.imgur.com/RZ2wo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RZ2wo.png" alt="enter image description here"></a>
  <strong>MAHASISWA TABLE</strong>
  <a href="https://i.stack.imgur.com/DhrvN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DhrvN.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <p><strong>Controller</strong></p>
  </blockquote>
  
  <pre><code>public function insertMahasiswa(){
      $username = $this-&gt;input-&gt;post('username');
      $password = md5($this-&gt;input-&gt;post('password'));
      $data1 = array(
              'username'=&gt;$username,
              'password'=&gt;$password,
              'level'=&gt;3,
          );
      $nim = $this-&gt;input-&gt;post('nim');
      $nama = $this-&gt;input-&gt;post('nama');
      $email = $this-&gt;input-&gt;post('email');
      $telepon = $this-&gt;input-&gt;post('telepon');
      $data = array(
              'nim'=&gt;$nim,
              'nama_mahasiswa'=&gt;$nama,
              'email'=&gt;$email,
              'telepon'=&gt;$telepon,
          );
      $insert = $this-&gt;MMahasiswa-&gt;create('pengguna',$data1);
      $insert1 = $this-&gt;MMahasiswa-&gt;create('mahasiswa',$data);
      redirect(site_url('mahasiswa/data?balasan=1'));
  }
  </code></pre>
  
  <blockquote>
    <p><strong>MODEL</strong></p>
  </blockquote>
  
  <pre><code>function create($table,$data){
      $query = $this-&gt;db-&gt;insert($table, $data);
      return $query;
  }
  </code></pre>