title: How do I insert the primary key to another table as foreign key?
body: <p>I have two tables 
  <code>tbl_cars</code> and <code>tbl_user</code> 
  Where <code>tbl_user</code> has <code>userID</code> as <code>Primary key</code> 
  I declared it as a Foreign key on my <code>tbl_cars</code> 
  Whenever a user logs in it can't post an item to the <code>tbl_cars</code> I get this error</p>
  
  <blockquote>
    <p>Cannot add or update a child row: a foreign key constraint fails
    (u850332371_car.tbl_cars, CONSTRAINT tbl_cars_ibfk_1 FOREIGN KEY
    (userID) REFERENCES tbl_user (userID))</p>
  </blockquote>
  
  <p>This is my code for inserting.</p>
  
  <h2>Insert.php</h2>
  
  <pre><code>&lt;?PHP
  $conn = new mysqli('******', '******', '******', '******');
  
   if ($conn-&gt;connect_error) {
   die("Connection failed: " . $conn-&gt;connect_error);
   }
  
  error_reporting(E_ALL);
  ini_set('display_errors',1);// at top of page
  
  if(isset($_POST['txtCarModel']) &amp;&amp; isset($_POST['txtCarType']) &amp;&amp; 
  isset($_POST['txtCapacity']) &amp;&amp; isset($_POST['image']) &amp;&amp;
  isset($_POST['txtFuelType']) &amp;&amp; isset($_POST['txtPlateNumber'])){
  
  $now = DateTime::createFromFormat('U.u', microtime(true));
  $id = $now-&gt;format('YmdHis');
  
  $upload_folder = "upload";
  $path = "$upload_folder/$id.jpeg";
  $image = $_POST['image'];
  $fullpath = "http://carkila.esy.es/$upload_folder/$id.jpeg";
  
  $Car_Model = $_POST['txtCarModel'];
  $Car_Type = $_POST['txtCarType'];
  $Capacity = $_POST['txtCapacity'];
  $Fuel_Type = $_POST['txtFuelType'];
  $PlateNumber = $_POST['txtPlateNumber'];
  $Image = $_POST['image'];
  
  
  $stmt = $conn-&gt;prepare("INSERT INTO tbl_cars (Car_Model, Car_Type, Capacity, fuelType, carPlatenuNumber, Image) VALUES (?, ?, ?,?,?,?)");
  $query = "INSERT INTO tbl_cars(Car_Model, Car_Type, Capacity,fuelType, carPlatenuNumber, Image) 
  VALUES ('$Car_Model', '$Car_Type', $Capacity, '$Fuel_Type', '$PlateNumber', '$fullpath')"; 
  
  $stmt-&gt;bind_param("ssssss", $Car_Model, $Car_Type, $Capacity,$Fuel_Type,$PlateNumber, $fullpath);
  $result = $stmt-&gt;execute();
  
  if($result === false ) {
    die('execute() failed: ' . htmlspecialchars($stmt-&gt;error));
  }else{
  echo "New records created successfully";
  }
  
  $stmt-&gt;close();
  $conn-&gt;close();
  
      }
  
  
  ?&gt;
  </code></pre>
  
  <h2>UPDATE</h2>
  
  <p>This is my login with sessions. I want the userID to be in the insertion of data to the database.</p>
  
  <h2>login.php</h2>
  
  <pre><code>&lt;?php 
  require 'database-config.php';
  
  session_start();
  
  $username = "";
  $password = "";
  
  if(isset($_POST['username'])){
  $username = $_POST['username'];
  }
  if (isset($_POST['password'])) {
  $password = $_POST['password'];
  
  }
  
  
  $q = 'SELECT * FROM tbl_user WHERE username=:username AND password=:password';
  
  $query = $dbh-&gt;prepare($q);
  
  $query-&gt;execute(array(':username' =&gt; $username, ':password' =&gt; $password));
  
  
  if($query-&gt;rowCount() == 0){
  header('Location: index.php?err=1');
  }else{
  
  $row = $query-&gt;fetch(PDO::FETCH_ASSOC);
  
  session_regenerate_id();
   $_SESSION['sess_user_id'] = $row['userID'];
   $_SESSION['sess_username'] = $row['username'];
   $_SESSION['sess_userrole'] = $row['roles'];
  
   echo $_SESSION['sess_userrole'];
  session_write_close();
  
  if( $_SESSION['sess_userrole'] == "renter"){
  echo "owner";
  
  }else if ($_SESSION['sess_userrole'] == "owner"){
  echo"renter";
  
  }
  
  
  }
  
  
  ?&gt;
  </code></pre>
  
  <p>Thank you guys. :)</p>