title: Insert from one form to two tables using PDO
body: <p>How to insert a form to two MySQL Database using the PDO here is my code:
  <strong>pc_owner table:</strong>
  pcid
  pcname,
  pcphone</p>
  
  <p><strong>pc_wo table:</strong>
  woid,
  pcid,
  probdesc</p>
  
  <blockquote>
  <pre><code>&lt;?php include('core/init.php'); ?&gt;
  
  &lt;?php
  //Create DB Object
  $db = new Database;
  
  //Run Query
  $db-&gt;query("INSERT INTO `pc_owner` (pcname, pcphone)
                                VALUES (:pcname, :pcphone)
            INSERT INTO `pc_wo` (probdesc)
                                VALUES (:probdesc,lastInsertId())");
  
  
  //Bind Values pc_owner
  $db-&gt;bind(':probdesc', $_POST['probdesc']);
  $db-&gt;bind(':pcphone', $_POST['pcphone']);
  //bidn values to pc_wo table
  $db-&gt;bind(':probdesc', $_POST['probdesc']);
  
  
  //$db-&gt;bind(':pcphone', $_POST['probdesc']);
  
  if($db-&gt;execute()){
    echo "Contact was added";
  } else {
    echo "Could not add contact";
  }
  
  ?&gt;
  </code></pre>
  </blockquote>
  
  <p>Database.php  class used </p>
  
  <pre><code>&lt;?php
  class Database {
      private $host = DB_HOST;
      private $user = DB_USER;
      private $pass = DB_PASS;
      private $dbname = DB_NAME;
  
      private $dbh;
      private $error;
      private $stmt;
  
      public function __construct() {
          // Set DSN
          $dsn = 'mysql:host=' . $this-&gt;host . ';dbname=' . $this-&gt;dbname;
          // Set options
          $options = array (
                  PDO::ATTR_PERSISTENT =&gt; true,
                  PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION 
          );
          // Create a new PDO instanace
          try {
              $this-&gt;dbh = new PDO ($dsn, $this-&gt;user, $this-&gt;pass, $options);
          }       // Catch any errors
          catch ( PDOException $e ) {
              $this-&gt;error = $e-&gt;getMessage();
          }
      }
  
  
      public function query($query) {
          $this-&gt;stmt = $this-&gt;dbh-&gt;prepare($query);
      }
  
  
      public function bind($param, $value, $type = null) {
          if (is_null ( $type )) {
              switch (true) {
                  case is_int ( $value ) :
                      $type = PDO::PARAM_INT;
                      break;
                  case is_bool ( $value ) :
                      $type = PDO::PARAM_BOOL;
                      break;
                  case is_null ( $value ) :
                      $type = PDO::PARAM_NULL;
                      break;
                  default :
                      $type = PDO::PARAM_STR;
              }
          }
          $this-&gt;stmt-&gt;bindValue ( $param, $value, $type );
      }
  
  
      public function execute(){
          return $this-&gt;stmt-&gt;execute();
      }
  
  
      public function resultset(){
          $this-&gt;execute();
          return $this-&gt;stmt-&gt;fetchAll(PDO::FETCH_OBJ);
      }
  
  
      public function single(){
          $this-&gt;execute();
          return $this-&gt;stmt-&gt;fetch(PDO::FETCH_OBJ);
      }
  
  
      public function rowCount(){
          return $this-&gt;stmt-&gt;rowCount();
      }
  
  
      public function lastInsertId(){
          return $this-&gt;dbh-&gt;lastInsertId();
      }
  
  
      public function beginTransaction(){
          return $this-&gt;dbh-&gt;beginTransaction();
      }
  
  
      public function endTransaction(){
          return $this-&gt;dbh-&gt;commit();
      }
  
  
      public function cancelTransaction(){
          return $this-&gt;dbh-&gt;rollBack();
      }
  }
  </code></pre>
  
  <p>How to use this class to insert to 2 tables</p>