title: Insert input value into two different tables with the same userId
body: <p>I need to send the values of username, password, mail, first name and last name into two different tables in my database. I have made that possible with this code, and i get the values correct. I have one problem tho. When the user-info goes into "users" table, it auto-generates one userId. That same userId i need to use and put into the other table "usermeta" to connect the information to the correct user. What happens now is that i get a userId in the users table, but in the usermeta table the userId is = 0 on all new users. Can anyone help me?</p>
  
  <pre><code>if (isset($_POST['username']) and isset ($_POST['password'])) {
      $username   = $mysqli-&gt;real_escape_string($_POST['username']);
      $password   = md5($mysqli-&gt;real_escape_string($_POST['password']));
      $email      = $mysqli-&gt;real_escape_string($_POST['mail']);
      $fname      = $mysqli-&gt;real_escape_string($_POST['fname']);
      $lname      = $mysqli-&gt;real_escape_string($_POST['lname']);
  
      $query = &lt;&lt;&lt;END
      INSERT INTO users(username,pass,email,admin)
      VALUES('{$username}','{$password}','{$email}','{$_POST['admin']}')
  END;
  
      $q = &lt;&lt;&lt;END
      INSERT INTO usermeta(first_name,last_name)
      VALUES('{$fname}','{$lname}')
  END;
  
  if ($mysqli-&gt;query($query) !== TRUE) {
      die("Could not query database" . $mysqli-&gt;errno . " : " . $mysqli-&gt;error);
      }
  if ( $mysqli-&gt;query($q) !== TRUE) {
      die("Could not query database" . $mysqli-&gt;errno . " : " . $mysqli-&gt;error);
      header('Location:home.php');
      } 
  }
  </code></pre>
  
  <p>I got a form further down in the code that the info comes from that's going to the database, but it's the php-code that makes the error i assume to i exclude that code.</p>
  
  <p>Thanks!</p>
  
  <p>EDIT:</p>
  
  <p>I've tried to implement your different comments. Thanks alot for the answers!
  If i try to get the latest id inbetween the $query and $q, the output is 0. If i use the command:</p>
  
  <pre><code>if ($mysqli-&gt;query($query) === TRUE) {
      $last_id = $mysqli-&gt;insert_id;
      echo $last_id;
  } else {
      echo "Error: " . $query . "&lt;br&gt;" . $mysqli-&gt;error;
  }
  </code></pre>
  
  <p>I get the correct id. But how can i use that variable to the new $q? It seems that the function breaks after the if is done.</p>
  
  <p>Thanks! :) </p>