title: How to execute multiple SQL query to different table?
body: <p>I'm attempting to run both query to different table.</p>
  <p>As <code>tbl_invoice</code> are inserted, <code>invoice_id</code> is created from first query (first one works) but when I put another query for <code>tbl_invoice_details</code> I think it doesn't actually pass the previous <code>invoice_id</code> value, tried with <code>if($invoice_id != null){...}</code> but this query doesn't seem to work for <code>tbl_invoice_details</code>.</p>
  <p>Any help would be appreciated, thanks.</p>
  <pre><code>
  include_once 'connectdb.php';
  
  $id = $_GET['id'];
  $select = $pdo-&gt;prepare(&quot;SELECT * FROM tbl_product WHERE barcode=$id&quot;);
  $select-&gt;execute();
  $row = $select-&gt;fetch(PDO::FETCH_OBJ);
  
      // FOR TBL_INVOICE
      $variables
      ... 
  
      $insert = $pdo-&gt;prepare(&quot;INSERT INTO tbl_invoice(customer_name,order_date,subtotal,tax,discount,total,paid,due,payment_type,profit) values(:cust,:orderdate,:stotal,:tax,:disc,:total,:paid,:due,:ptype,:profit)&quot;);
  
      $insert-&gt;bindParam(':cust',$customer_name);
      $insert-&gt;bindParam(':orderdate',$order_date);
      $insert-&gt;bindParam(':stotal',$subtotal);
      $insert-&gt;bindParam(':tax',$tax);
      $insert-&gt;bindParam(':disc',$discount);
      $insert-&gt;bindParam(':total',$total);
      $insert-&gt;bindParam(':paid',$paid);
      $insert-&gt;bindParam(':due',$due);
      $insert-&gt;bindParam(':ptype',$payment_type);
      $insert-&gt;bindParam(':profit',$profit);
      $insert-&gt;execute();
  
      // FOR TBL_INVOICE_DETAILS
      
      $invoice_id = $pdo-&gt;lastInsertId();
      if($invoice_id != null){
      $rem_qty = $stock - $qty;
      $update = $pdo-&gt;prepare(&quot;UPDATE tbl_product SET pstock='$rem_qty' WHERE barcode='&quot;.$id.&quot;'&quot;);
      $update-&gt;execute();
          
      $insert = $pdo-&gt;prepare(&quot;INSERT INTO tbl_invoice_details(invoice_id,product_id,product_name,qty,price,order_date,values(:invid,:pid,me,:qty,:price,:orderdate,:profit)&quot;);
      
      $insert-&gt;bindParam(':invid',$invoice_id);
      $insert-&gt;bindParam(':pid',$pid);
      $insert-&gt;bindParam(':pname',$productname);
      $insert-&gt;bindParam(':qty',$qty);
      $insert-&gt;bindParam(':price',$total);
      $insert-&gt;bindParam(':orderdate',$order_date);
      $insert-&gt;bindParam(':profit',$profit);
      $insert-&gt;execute();
      }
      
      
  ?&gt;```
  </code></pre>