MySQL : Cannot add or update a child row: a foreign key constraint fails when insert my data to database in Codeigniter
<p>I got an error like this : Cannot add or update a child row: a foreign key constraint fails when insert my data to database phpmyadmin using Codeigniter.</p>
<p><strong>This is my error message :</strong>
<br></p>
<blockquote>
<p>Cannot add or update a child row: a foreign key constraint fails (<code>tugasakhir</code>.<code>tb_pasien</code>, CONSTRAINT <code>tb_pasien_ibfk_1</code> FOREIGN KEY (<code>kode_pasien</code>) REFERENCES <code>tb_jk</code> (<code>kode_jk</code>) ON DELETE CASCADE ON UPDATE CASCADE)</p>
<p>INSERT INTO <code>tb_pasien</code> (<code>nama_pasien</code>, <code>email_pasien</code>, <code>username</code>, <code>password</code>, <code>alamat_pasien</code>, <code>tanggal_lahir</code>, <code>umur</code>, <code>kode_jk</code>, <code>no_telp</code>, <code>no_antrian</code>) VALUES ('nandoku', 'gustiayuri@gmail.com', 'nandoku', '827ccb0eea8a706c4c34a16891f84e7b', 'dalung', '11/12/1995', '21', 'L', '567890', 0)</p>
</blockquote>
<p><strong>This is my controller :</strong>
<br></p>
<pre><code>public function insert()
{      
    $this-&gt;load-&gt;database();
    $this-&gt;load-&gt;model('p_model');

    $data_pasien = array(
        &quot;nama_pasien&quot; =&gt; $this-&gt;input-&gt;post('nama_pasien'),
        &quot;email_pasien&quot; =&gt; $this-&gt;input-&gt;post('email_pasien'),
        &quot;username&quot; =&gt; $this-&gt;input-&gt;post('username'),
        &quot;password&quot; =&gt; md5($this-&gt;input-&gt;post('password')),
        &quot;alamat_pasien&quot; =&gt; $this-&gt;input-&gt;post('alamat_pasien'),
        &quot;tanggal_lahir&quot; =&gt; $this-&gt;input-&gt;post('tanggal_lahir'),
        &quot;umur&quot; =&gt; $this-&gt;input-&gt;post('umur'),
        &quot;kode_jk&quot; =&gt; $this-&gt;input-&gt;post('kode_jk'),
        &quot;no_telp&quot; =&gt; $this-&gt;input-&gt;post('no_telp'),
        &quot;no_antrian&quot; =&gt; $this-&gt;input-&gt;post('no_antrian')
    );

    $data_jk = array(
      'nama_jk'       =&gt; $this-&gt;input-&gt;post('nama_jk')
    );    

    $this-&gt;p_model-&gt;insert_entry($data_pasien, $data_jk);
}
</code></pre>
<p><strong>Model :</strong>
<br></p>
<pre><code>    function insert_entry($data_pasien, $data_jk) {

    $this-&gt;db-&gt;insert('tb_pasien', $data_pasien);

    $data_pasien['kode_jk'] = $this-&gt;db-&gt;insert_id();

    $this-&gt;db-&gt;insert('nama_jk', $data_pasien);

}
</code></pre>
<p><strong>View</strong>
<br></p>
<pre><code> &lt;!-- form start --&gt;
        &lt;form role=&quot;form&quot; action=&quot;&lt;?php echo base_url().'index.php/a_controller/tambahmanual' ?&gt;&quot; method=&quot;post&quot;&gt;
          &lt;div class=&quot;box-body&quot;&gt;

            &lt;div class=&quot;form-group&quot;&gt;
              &lt;label for=&quot;exampleInputNama&quot;&gt;Nama Pasien&lt;/label&gt;
                &lt;div class=&quot;input-group&quot;&gt;
                    &lt;span class=&quot;input-group-addon&quot;&gt;&lt;i class=&quot;fa fa-user&quot;&gt;&lt;/i&gt;&lt;/span&gt;
                    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;nama_pasien&quot; id=&quot;exampleInputNama&quot; placeholder=&quot;Nama Pasien&quot;&gt;
                &lt;/div&gt;
            &lt;/div&gt;

            &lt;div class=&quot;form-group&quot;&gt;
                &lt;label for=&quot;exampleInputEmail&quot;&gt;Email Pasien&lt;/label&gt;
                &lt;div class=&quot;input-group&quot;&gt;
                    &lt;span class=&quot;input-group-addon&quot;&gt;&lt;i class=&quot;fa fa-envelope&quot;&gt;&lt;/i&gt;&lt;/span&gt;
                    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;email_pasien&quot; id=&quot;exampleInputEmail&quot; placeholder=&quot;Email Pasien&quot;&gt;
                &lt;/div&gt;
            &lt;/div&gt;

            &lt;div class=&quot;form-group&quot;&gt;
              &lt;label for=&quot;exampleInputUsername&quot;&gt;Username&lt;/label&gt;
                &lt;div class=&quot;input-group&quot;&gt;
                    &lt;span class=&quot;input-group-addon&quot;&gt;&lt;i class=&quot;fa fa-user&quot;&gt;&lt;/i&gt;&lt;/span&gt;
                    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;username&quot; id=&quot;exampleInputUsername&quot; placeholder=&quot;Username&quot;&gt;
                &lt;/div&gt;
            &lt;/div&gt;

            &lt;div class=&quot;form-group&quot;&gt;
              &lt;label for=&quot;exampleInputPassword&quot;&gt;Password&lt;/label&gt;
              &lt;div class=&quot;input-group&quot;&gt;
                    &lt;span class=&quot;input-group-addon&quot;&gt;&lt;i class=&quot;fa fa-key&quot;&gt;&lt;/i&gt;&lt;/span&gt;
                    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;password&quot; id=&quot;exampleInputPassword&quot; placeholder=&quot;Password&quot;&gt;
                &lt;/div&gt;
            &lt;/div&gt;

            &lt;div class=&quot;form-group&quot;&gt;
              &lt;label for=&quot;exampleInputAlamat&quot;&gt;Alamat&lt;/label&gt;
                &lt;div class=&quot;input-group&quot;&gt;
                    &lt;span class=&quot;input-group-addon&quot;&gt;&lt;i class=&quot;fa fa-home&quot;&gt;&lt;/i&gt;&lt;/span&gt;
                    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;alamat_pasien&quot; id=&quot;exampleInputAlamat&quot; placeholder=&quot;Alamat&quot;&gt;
                &lt;/div&gt;
            &lt;/div&gt;

            &lt;div class=&quot;form-group&quot;&gt;
            &lt;label&gt;Tanggal lahir&lt;/label&gt;

            &lt;div class=&quot;input-group date&quot;&gt;
              &lt;div class=&quot;input-group-addon&quot;&gt;
                &lt;i class=&quot;fa fa-calendar&quot;&gt;&lt;/i&gt;
              &lt;/div&gt;
              &lt;input type=&quot;date&quot; name=&quot;tanggal_lahir&quot; class=&quot;form-control&quot;&gt;
            &lt;/div&gt;
            &lt;!-- /.input group --&gt;
            &lt;/div&gt;

            &lt;div class=&quot;form-group&quot;&gt;
              &lt;label for=&quot;exampleInputUmur&quot;&gt;Umur&lt;/label&gt;
                &lt;div class=&quot;input-group&quot;&gt;
                    &lt;span class=&quot;input-group-addon&quot;&gt;&lt;i class=&quot;fa fa-user&quot;&gt;&lt;/i&gt;&lt;/span&gt;
                    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;umur&quot; id=&quot;exampleInputUmur&quot; placeholder=&quot;Umur&quot;&gt;
                &lt;/div&gt;
            &lt;/div&gt;

            &lt;div class=&quot;form-group&quot;&gt;
              &lt;label&gt;Jenis Kelamin&lt;/label&gt;
              &lt;select name=&quot;kode_jk&quot; class=&quot;form-control&quot;&gt;
                &lt;option value=&quot;1&quot;&gt;L&lt;/option&gt;
                &lt;option value=&quot;2&quot;&gt;P&lt;/option&gt;
              &lt;/select&gt;
            &lt;/div&gt;

            &lt;div class=&quot;form-group&quot;&gt;
              &lt;label for=&quot;exampleInputTelp&quot;&gt;No. Telp&lt;/label&gt;
                &lt;div class=&quot;input-group&quot;&gt;
                    &lt;span class=&quot;input-group-addon&quot;&gt;&lt;i class=&quot;fa fa-phone&quot;&gt;&lt;/i&gt;&lt;/span&gt;
                    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; name=&quot;no_telp&quot; id=&quot;exampleInputTelp&quot; placeholder=&quot;No. Telp&quot;&gt;
                &lt;/div&gt;
            &lt;/div&gt;


            

          &lt;/div&gt;
          &lt;!-- /.box-body --&gt;

          &lt;div class=&quot;box-footer&quot;&gt;
            &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Submit&lt;/button&gt;
          &lt;/div&gt;
        &lt;/form&gt;
</code></pre>
<p>This is my tables :</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>CREATE TABLE `tb_pasien` (
  `kode_pasien` int(4) NOT NULL AUTO_INCREMENT,
  `nama_pasien` varchar(20) NOT NULL,
  `email_pasien` varchar(20) NOT NULL,
  `username` varchar(20) NOT NULL,
  `password` varchar(255) NOT NULL,
  `alamat_pasien` text NOT NULL,
  `tanggal_lahir` date NOT NULL,
  `umur` int(2) NOT NULL,
  `kode_jk` int(2) NOT NULL,
  `no_telp` int(20) NOT NULL,
  `no_antrian` int(3) NOT NULL,
  PRIMARY KEY (`kode_pasien`),
  CONSTRAINT `tb_pasien_ibfk_1` FOREIGN KEY (`kode_pasien`) REFERENCES `tb_jk` (`kode_jk`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1


CREATE TABLE `tb_jk` (
  `kode_jk` int(2) NOT NULL AUTO_INCREMENT,
  `nama_jk` varchar(1) NOT NULL,
  PRIMARY KEY (`kode_jk`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1</code></pre>
</div>
</div>
</p>
<p><a href="https://i.stack.imgur.com/vtdpU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vtdpU.jpg" alt="value for my tb_jk" /></a> <br>
Note : <em>kode_jk is foreign key on tb_pasien table.</em><br>
What should i do for this error message?</p>