title: PHP MySQLi prepared statement not inserting data into table
body: <p>I am designing a website with user login and registration with email verification, and when the user registers an account, it will successfully, and correctly, insert the user record into the database in the "users" table.</p>
  
  <p>But when I try to pull the users id that is created when inserted to insert them into a second table for a roster of all members of the site (this is for a gaming clan), it inserts the user id as 0 in the roster table, regardless of what their id is on the "users" table for that user.</p>
  
  <p>heres my code:</p>
  
  <pre><code>if($insert_stmt = $db-&gt;prepare("INSERT INTO users (username, password, email, date, actcode) VALUES (?, ?, ?, ?, ?)"))
      {
          $insert_stmt-&gt;bind_param('sssss', $username, $password, $email, $date, $actcode);
  
          $stmt = $db-&gt;prepare("SELECT id FROM users WHERE email = ?");
          if($stmt)
          {
              $stmt-&gt;bind_param('s', $email);
              $stmt-&gt;execute();
              $stmt-&gt;fetch();
              $stmt-&gt;store_result();
              $stmt-&gt;bind_result($ui);
          }
  
          $stmt = $db-&gt;prepare("INSERT INTO roster (userid, joindate) VALUES (?, ?)");
          if($stmt)
          {
              $stmt-&gt;bind_param('ss', $ui, $date);
              $stmt-&gt;execute();
          }
  
          if(!$insert_stmt-&gt;execute()) {
              header('Location: error.php?err=Registration failure: INSERT');
          }
      }
  </code></pre>
  
  <p>for security reasons I have left certain variables and sections of the code omitted that do not have any bearing on this section causing me a headache.</p>
  
  <p>I can't figure out why it is not inserting the newly creating "id" from "users" for that user account into the "roster" table under the "userid" column.</p>
  
  <p>Also, just to test something, I also went and set a session variable</p>
  
  <p><code>$_SESSION['uid'] = $ui;</code></p>
  
  <p>directly after the line</p>
  
  <pre><code>$stmt-&gt;store_result($ui);
  </code></pre>
  
  <p>and echoed it on my index.php file, and it shows the session variable as 0 as well.</p>