title: Saving data into two tables at the same time
body: <p>I have this code</p>
  
  <pre><code>&lt;?php
      include 'dbconnect.php'; ?&gt;
  &lt;head&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;form method="post" action="&lt;?php echo $_SERVER["PHP_SELF"]?&gt;"&gt;
      &lt;input type="text" name="product_name"/&gt;
      &lt;input type="text" name="product_price" /&gt;
  
          &lt;select name="product_cat"&gt;
          &lt;?php
          $results = $connect-&gt;query("SELECT * FROM categories ORDER BY cat_name");
              while($row = $results-&gt;fetch_array()) {
              extract($row);
              echo "&lt;option value='"."{$cat_name}"."'&gt;"."{$cat_name}"."&lt;/option&gt;";
              }  
          $results-&gt;free();
          ?&gt;
          &lt;/select&gt;
  
      &lt;input type="submit" name="submit" value="Submit"&gt;
      &lt;/form&gt;
  &lt;?php   
      if($_POST){
      $sql = "INSERT INTO products (prd_name, prd_price, cat_id) VALUES(?,?,?)";
      if($stmt = $connect-&gt;prepare($sql) )
          {
              $productname = $_POST['product_name'];
              $productprice = $_POST['product_price'];
              $productcat = $_POST['product_cat'];
              $stmt-&gt;bind_param("sii", $productname, $productprice, $productcat);
          if($stmt-&gt;execute())
              {
                  echo "Created";
                  $stmt-&gt;close();
              }else{
                      die("Unable to create category.");
                  }
                  }else{
                  die("Unable to prepare statement.");
              }
                  $connect-&gt;close();
              }
          ?&gt;    
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here the category name is called from a table called categories. You can see the code at the dropdown. Once the user select the category name, fills the product name and product price the form saves the all three data to a table called products. </p>
  
  <p>There is a third table called category_product with two column prd_id and cat_id (product id and category id). I need to record both the id's of product and the category to this table along with this form. </p>
  
  <p>In short, how do i get the category id from the category table when only the name is being echoed and how to know the last saved id of the product and then inserting both the records into the second(category_products) tables at the same time.</p>