title: How to fix "Cannot add or update a child row: a foreign key constraint fails"
body: <p>I'm trying to insert into two tables (parent and child) at the same time from php. This is how I programmed it; I'll insert a unique number together with my original data into the parent table then use the same unique number to fetch the last data I inserted just to get the "id" of the recent data, so I can use it in the child table. It inserts successfully into the first table but did not insert into the second table. I'm getting this error:</p>
  
  <blockquote>
    <p>Cannot add or update a child row: a foreign key constraint fails (<code>attendance_db</code>.<code>attendance_tb</code>, CONSTRAINT <code>attendance_tb_ibfk_1</code> FOREIGN KEY (<code>student_id</code>) REFERENCES <code>student_tb</code> (<code>id</code>) ON DELETE CASCADE ON UPDATE CASCADE).</p>
  </blockquote>
  
  <p>How do I insert into the two tables successfully? Any other method will be accepted. Thanks.</p>
  
  <p>NOTE: I created the relation from the designer menu in phpmyadmin.</p>