title: return mysql last insert id as a link variable
body: <p>I have the following php pages. I need to return the last inserted id of the INSERT statement to my php page as a link variable. My code is as follows:</p>
  
  <p>room_reservation.php</p>
  
  <pre><code>&lt;?php
      if(isset($_REQUEST["id"])){
          echo $_REQUEST["id"];
      }
  ?&gt;
  &lt;html&gt;
  &lt;body&gt;
      &lt;form method="post" action="reception-c.php"&gt;
          &lt;input type="text" name="trans_id"/&gt;
          &lt;input type="text" name="start_date"/&gt;
          &lt;input type="text" name="cust_id"/&gt;
          &lt;input type="submit" name="submit" value="submit"/&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>reception-c.php</p>
  
  <pre><code>&lt;?php
      require_once '../model/reception-m.php';
  
      $trans_id=$_POST["trans_id"];
      $start_date=$_POST["apply_date"];
      $cust_id=$_POST["customerid"];
      $obj=new Reception();
      if($trans_id===""){
          $result=$obj-&gt;generateNewTransaction($start_date,$cust_id);
          $trans_id=$result;
      }
      header("Location: ../view/room_reservation.php?id=$trans_id");
  </code></pre>
  
  <p>?></p>
  
  <p>reception-m.php</p>
  
  <pre><code>require_once '../../common/model/dbconnection-m.php';
  
  &lt;?php
  class Reception{
  public function generateNewTransaction($start_date,$cust_id){
      global $db;
      $query="INSERT INTO transaction_log VALUES ('','$start_date',NULL,'pending','$cust_id')";
      $result=$db-&gt;add_query($query);
      return $db-&gt;insert_id; //Not Working
  }
  }
  ?&gt;
  </code></pre>
  
  <p>dbconnection-m.php</p>
  
  <pre><code>&lt;?php
  
  class Database{
  private $connection;
  
  function __construct() {
      $this-&gt;open_connection();
  }
  
  public function open_connection(){
      $this-&gt;connection=  mysqli_connect('localhost', 'root', '', 'mis');
          if(!$this-&gt;connection){
              die("Error, failed to connect to database ".  mysqli_error($this-&gt;connection));
          }         
  }
  
  public function add_query($query){
      $result=  mysqli_query($this-&gt;connection, $query);
      if(!$result){
          die("Error while adding query ".  mysqli_error($this-&gt;connection));
      }
      else{
          return $result;
      }
  }
  
  public function fetch_array($value){
      return $value-&gt;fetch_assoc();
  }
  }
  
  $db=new Database(); 
  </code></pre>
  
  <p>My code works up until the point where in reception-m.php the query gets submitted. Confirmed through phpmyadmin browse of table. However return $db->insert_id; in reception-m .php does not work and I get an empty value returned (not zero). </p>
  
  <p>The table has transaction_id set to AUTO INCREMENT so I don't think that's the issue. I am concerned with my dbconnection-m.php as this is not the one used in explanations of php documentation.</p>
  
  <p>Please help me figure out this issue...</p>
  
  <p>EDIT</p>
  
  <pre><code>1   transaction_id Primary  int(11) AUTO_INCREMENT  
  2   start_date  date
  3   end_date    date
  4   status  enum('pending', 'completed')    
  5   customer_id int(11)
  </code></pre>