title: MySQL Error: Cannot add or update a child row: a foreign key constraint fails
body: <p>So the scenario is this:</p>
  
  <p>I create a group with a form, stored in test_groups_tb, then with another form enter user ids to attach them to the group and they are stored in group_association_tb with a structure like so:</p>
  
  <p><code>group_id | user_id</code></p>
  
  <p>I am very new to MySQL and more so Foreign Key relationships, so I tried to set up a relationship on group_association_tb which was basically, if the group was deleted, delete all records of that group from group_association_tb.</p>
  
  <p>now what Is happening since applying this is the group is stored fine. I can add students manually fine, but when I try adding them by importing a csv it doesn't like it.</p>
  
  <p>here is the queries (i know its not efficient querying every iteration):</p>
  
  <pre><code>while($row = mysql_fetch_assoc($result))
      {
          $sql = "INSERT INTO group_association_tb (group_id, user_id)VALUES('$group','".$row[user_id]."')";
          mysql_query($sql) or die(header("Location:error.php"));
      } 
  </code></pre>
  
  <p>here is the error i get:</p>
  
  <pre><code>Cannot add or update a child row: a foreign key constraint fails (`ece70141/group_association_tb`, CONSTRAINT `group_association_tb_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `test_groups_tb` (`group_id`) ON DELETE CASCADE)
  </code></pre>
  
  <p>could somebody explain what the problem is here.</p>
  
  <p>many thanks,</p>