title: INSERT, and how to INSERT into multiple tables
body: <p>So I have this form:
  <a href="https://i.imgur.com/vZYssQy.png" rel="nofollow noreferrer">http://i.imgur.com/vZYssQy.png</a></p>
  
  <p>Now the <code>ID</code>, <code>First Name</code>, <code>Last Name</code>, <code>DOB</code>, <code>Address</code>, <code>Phone Number</code> and <code>Post Code</code> text boxes are supposed to INSERT into a table called <code>person</code> (and this works, it inserts)</p>
  
  <p>My code for this is:</p>
  
  <pre><code>public static void insertStudent(int personId, string firstName, string lastName, string DOB, int phoneNumber, string address, int postCode, string majorField, int gradePointAverage)
  {
      MySqlConnection conn = connection();
      conn.Open();
      MySqlCommand cmd = new MySqlCommand();
      cmd.Connection = conn;
      string myInsertSQL = "INSERT INTO person(personId, firstName, lastName, DOB, phoneNumber, address, postCode) VALUES (@personId, @firstName, @lastName, @DOB, @phoneNumber, @address, @postCode)";
      cmd.Prepare();
      cmd.CommandText = myInsertSQL;
      cmd.Parameters.AddWithValue("@personId", personId);
      cmd.Parameters.AddWithValue("@firstName", firstName);
      cmd.Parameters.AddWithValue("@lastName", lastName);
      cmd.Parameters.AddWithValue("@DOB", DOB);
      cmd.Parameters.AddWithValue("@phoneNumber", phoneNumber);
      cmd.Parameters.AddWithValue("@address", address);
      cmd.Parameters.AddWithValue("@postCode", postCode);
      cmd.ExecuteNonQuery();
  }
  </code></pre>
  
  <p>But the other two text boxes, <code>GPA</code> and <code>Major Field</code> need to go to a separate table called <code>student</code>. And the code I have for this is:</p>
  
  <pre><code> string myInsertSQLStudent = "INSERT INTO student(majorField, gradePointAverage) VALUES (@majorField, @gradePointAverage)";
              cmd.Prepare();
              cmd.CommandText = myInsertSQLStudent;
              cmd.Parameters.AddWithValue("@majorField", majorField);
              cmd.Parameters.AddWithValue("@gradePointAverage", gradePointAverage);
              prevID(conn, cmd);
  </code></pre>
  
  <p>which is right underneath the above code. this does not work however, I get a crash and a message telling me <code>Cannot add or update a child row: a foreign key constraint fails</code> then it talks about the primary key.</p>
  
  <p>however, it works KIND OF, if I fill out the text boxes, and click <code>insert</code> the program crashes, but the <code>person</code> table gets filled with the data, but the <code>student</code> table does not.</p>
  
  <p>How would I go about making it work?</p>
  
  <p>Thank you!</p>
  
  <p>EDIT:</p>
  
  <p>This is my person table:</p>
  
  <pre><code>personId | firstName | lastName | DOB | phoneNumber | address | postCode |
  
  blah     |blah       |blah      |blah |blah         |blah     |blah      |blah
  </code></pre>
  
  <p>this is my student table</p>
  
  <pre><code>person_personId | majorField | gradePointAverage |
  balh            |blah        |blah               |
  </code></pre>