title: How do i insert data parent table and child table same time?
body: <p>Im trying to insert data into mysql using codeIgniter framework when when i try it will showing this error</p>
  
  <pre><code>    Error Number: 1452
  
  Cannot add or update a child row: a foreign key constraint fails (`pslsystem`.`invoice_description`, CONSTRAINT `invoice_description_ibfk_1` FOREIGN KEY (`reference_no`) REFERENCES `myinvoice` (`reference_no`) ON DELETE CASCADE ON UPDATE CASCADE)
  
  INSERT INTO `invoice_description` (`reference_no`, `description`) VALUES (6, 'description1')
  
  Filename: C:/xampp/htdocs/pslsystem/application/models/sample_model.php
  
  Line Number: 21
  </code></pre>
  
  <p>This is my two tables :</p>
  
  <pre><code>CREATE TABLE myinvoice (
      invoice_id INT  NOT NULL AUTO_INCREMENT,
      invoice_type  varchar(30) NOT NULL,
      reference_no varchar(30) NOT NULL UNIQUE,
      des_title varchar(300) NULL ,
      PRIMARY KEY (invoice_id, reference_no)
  
  ) ENGINE=InnoDB;
  
  CREATE TABLE invoice_description(
   des_id INT NOT NULL AUTO_INCREMENT,
   reference_no varchar(30) NOT NULL,
   description varchar(300) NOT NULL,
   PRIMARY KEY (des_id,reference_no),
   FOREIGN KEY (reference_no) REFERENCES myinvoice (reference_no) on delete cascade ON UPDATE CASCADE
  )ENGINE=InnoDB;
  </code></pre>
  
  <p>here is my model </p>
  
  <pre><code>function insert_entry($data1, $data2) {
  
      $this-&gt;db-&gt;insert('myinvoice', $data1);
  
      $data2['reference_no'] = $this-&gt;db-&gt;insert_id();
  
      $this-&gt;db-&gt;insert('invoice_description', $data2);
  }
  </code></pre>
  
  <p>Here is My controller:</p>
  
  <pre><code>if($this-&gt;form_validation-&gt;run())
          {
              $this-&gt;load-&gt;database();
              $this-&gt;load-&gt;model('sample_model');
              $data1 = array(
                    'invoice_type' =&gt; $this-&gt;input-&gt;post('type'),
                    'reference_no' =&gt; $this-&gt;input-&gt;post('ref'),
                    'des_title' =&gt; $this-&gt;input-&gt;post('title'),
  
                    );
  
          $data2 = array(
              'reference_no' =&gt; $this-&gt;input-&gt;post('ref'),
               'description' =&gt; $this-&gt;input-&gt;post('des'),
          );
  
               $this-&gt;sample_model-&gt;insert_entry($data1,$data2);
  
  
  
          }else{
  
              $this-&gt;load-&gt;view('sample_view');
          }
  </code></pre>
  
  <p>when i try to insert parent table data only inserting. and shoing this error.</p>