title: PHP MySQL Converting names to ID's and inserting is returning NULL values
body: <p>I have a form where the user can input the name (combined string of first and last names) of a member, student and/or instructor. They also input other data irrelevant to this question. This is all inserted into a table called <code>games</code>. A table called <code>members</code>, <code>students</code> and <code>instructors</code> already exists with fields including <code>id</code>, <code>first_name</code> and <code>last_name</code> for each. All the required foreign key constraints are in order. So when the user submits the form, I only want the corresponding id of the member, student and/or instructor to be inserted, not their name(s).</p>
  
  <p>I have the following code, and it successfully submits all information besides the id of the member, student and/or instructor, which appear in the record as NULL.</p>
  
  <p>If anyone could highlight the issue here I would greatly appreciate it.</p>
  
  <p>Form:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Form&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form action="action.php" method="post"&gt;
          &lt;p&gt;
      &lt;p&gt;
          &lt;label for="instructor_name"&gt;Instructor:&lt;/label&gt;
          &lt;input type="text" name="instructor_name" id="instructor_name"&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label for="student_name"&gt;Student:&lt;/label&gt;
          &lt;input type="text" name="student_name" id="student_name"&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label for="member_name"&gt;Member:&lt;/label&gt;
          &lt;input type="text" name="member_name" id="member_name"&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label for="exercises"&gt;Exercises:&lt;/label&gt;
          &lt;input type="text" name="exercises" id="exercises"&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label for="auth_by"&gt;Authorised By:&lt;/label&gt;
          &lt;input type="text" name="auth_by" id="auth_by"&gt;
      &lt;/p&gt;
      &lt;p&gt;
          &lt;label for="auth_duration"&gt;Authorisation Duration:&lt;/label&gt;
          &lt;input type="text" name="auth_duration" id="auth_duration"&gt;
      &lt;/p&gt;
      &lt;input type="submit" value="Submit"&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Action:</p>
  
  <pre><code>&lt;?php
  
  // connection information
  include 'pdo_config.php';
  
  try {
  // new pdo connection
  $conn = new PDO($dsn, $user, $pass, $opt);
  
  // post data
  $member_name = $_POST['member_name'];
  $exercises = $_POST['exercises'];
  $auth_by = $_POST['auth_by'];
  $auth_duration = $_POST['auth_duration'];
  $student_name = $_POST['student_name'];
  $instructor_name = $_POST['instructor_name'];
  
  // get corresponding id's of names
  $name_m = explode(" ", $member_name);
  $first_name_m = $name_m[0];
  $last_name_m = $name_m[1];
  $statement1 = $conn-&gt;prepare("SELECT member_id FROM tbl_members WHERE first_name = :first_name AND last_name = :last_name");
  $statement1-&gt;execute(array(':first_name' =&gt; $first_name_m, ':last_name' =&gt; $last_name_m));
  $row1 = $statement1-&gt;fetch();
  $member_id = $row1['member_id'];
  
  $name_i = explode(" ", $instructor_name);
  $first_name_i = $name_i[0];
  $last_name_i = $name_i[1];
  $statement2 = $conn-&gt;prepare("SELECT instructor_id FROM tbl_instructors WHERE first_name = :first_name AND last_name = :last_name");
  $statement2-&gt;execute(array(':first_name' =&gt; $first_name_i, ':last_name' =&gt; $last_name_i));
  $row2 = $statement2-&gt;fetch();
  $instructor_id = $row2['instructor_id'];
  
  $name_s = explode(" ", $student_name);
  $first_name_s = $name_s[0];
  $last_name_s = $name_s[1];
  $statement3 = $conn-&gt;prepare("SELECT student_id FROM tbl_students WHERE first_name = :first_name AND last_name = :last_name");
  $statement3-&gt;execute(array(':first_name' =&gt; $first_name_s, ':last_name' =&gt; $last_name_s));
  $row3 = $statement3-&gt;fetch();
  $student_id = $row3['student_id'];
  
  // prepare statements and bind parameters
  $stmt = $conn-&gt;prepare("INSERT INTO tbl_games (member_id, exercises, auth_by, auth_duration, student_id, instructor_id) VALUES (:member_id, :exercises, :auth_by, :auth_duration, :student_id, :instructor_id)");
  
  $stmt-&gt;bindParam(':member_id', $member_id);
  $stmt-&gt;bindParam(':exercises', $exercises);
  $stmt-&gt;bindParam(':auth_by', $auth_by);
  $stmt-&gt;bindParam(':auth_duration', $auth_duration);
  $stmt-&gt;bindParam(':student_id', $student_id);
  $stmt-&gt;bindParam(':instructor_id', $instructor_id);
  
  // execute statements
  $stmt-&gt;execute();
  
  // success or error message
  echo "New record created successfully";
  }
  catch(PDOException $e)
  {
  echo "Error: " . $e-&gt;getMessage();
  }
  
  $conn = null;
  
  ?&gt;
  </code></pre>