title: Form post array to class.php and PDO insert function in db.php
body: <p>I was wondering if you could help me with a problem when submitting a form to POST values and using a PDO Insert function to enter values into database. Once someone can help me find the issue I will be able to use code over again in form areas. I have checked my $conn PDO statement and it is connected correctly to database just I can not insert the data from form. </p>
  
  <p>My coding layout: 
  Form located in cust_form.php, names of form fields are as in database with the exception of an autoID generated upon insertion. 
  Class.php is used to take POST values and to send to Insert function located in db.php.</p>
  
  <p>db.php</p>
  
  <pre><code>&lt;?php
  //dbdt database class
  if(!class_exists('dbdt')){
      class dbdt {
  
          //Connect and select database
  
          function connect() {
              try {
                  require_once('config.php');
                      $conn = new PDO('mysql:host=localhost;dbname=displaytrends', $DB_USER, $DB_PASS);
                      $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                  } catch(PDOException $e) {
                      echo 'ERROR: ' . $e-&gt;getMessage();
                  }
          }
  
          //Connect to above
          function __construct() {
              $this-&gt;connect();
          }
  
  
          //Insert data into database
  
          function insert($conn, $table, $fields, $values) {
              try{
                  $fields = implode(", ", $fields);
                  $values = implode(", ", $values);
  
                  $insert = "INSERT INTO $table (autoID, $fields) VALUES ('', $values)";
                  $query = $handler-&gt;prepare($insert);
                  $query-&gt;execute();
              } catch(PDOException $e) {
                      echo 'ERROR: ' . $e-&gt;getMessage();
                  }   
          }
  
      }
  }
  
  $dbdt = new dbdt;
  ?&gt;
  </code></pre>
  
  <p>class.php</p>
  
  <pre><code>&lt;?php
  if(!class_exists('cust_form')){
      class cust_form {
  
  /*
  CUSTOMER FORM = cust_form.php
  */
  
      function cust_upd_cre_del(){
  
          if ( isset( $_POST['cust_upd'] ) ) {
                  $int_custID=$_POST['int_custID'];
                  $cust_company=$_POST['cust_company'];
                  $cust_address=$_POST['cust_address'];
                  $cust_postcode=$_POST['cust_postcode'];
                  $cust_contact_1=$_POST['cust_contact_1'];
                  $cust_contact_2=$_POST['cust_contact_2'];
                  $cust_tel=$_POST['cust_tel'];
                  $cust_mob=$_POST['cust_mob'];
                  $cust_DDI=$_POST['cust_DDI'];
                  $cust_email=$_POST['cust_email'];
                  $cust_notes=$_POST['cust_notes'];
              require_once('db.php');
              $table = 'customers';
              $fields = array(
              'int_custID', 
              'cust_company', 
              'cust_address', 
              'cust_postcode', 
              'cust_contact_1', 
              'cust_contact_2', 
              'cust_tel', 
              'cust_mob', 
              'cust_DDI',
              'cust_email',
              'cust_notes'
              );
              $values = array (
                  'int_custID' =&gt; $int_custID, 
                  'cust_company' =&gt; $cust_company, 
                  'cust_address' =&gt; $cust_address, 
                  'cust_postcode' =&gt; $cust_postcode, 
                  'cust_contact_1' =&gt; $cust_contact_1, 
                  'cust_contact_2' =&gt; $cust_contact_2, 
                  'cust_tel' =&gt; $cust_tel, 
                  'cust_mob' =&gt; $cust_mob, 
                  'cust_DDI' =&gt; $cust_DDI,
                  'cust_email' =&gt; $cust_email,
                  'cust_notes' =&gt; $cust_notes
              );
          $insert = $dbdt-&gt;insert($conn, $table, $fields, $values);
  
          if ( $insert == TRUE ) {
                      }
                  } else {
                      die('Your form was not submitted.');
                  }
          }
      }
  }
  $cust_form = new cust_form;
  ?&gt;
  </code></pre>
  
  <p>cust_form.php</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;?php
      require_once('load.php');
  ?&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;title&gt;Customer Form&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
          &lt;form action="" method="POST" name="cust_details_form" id="cust_details_form"&gt;   
             &lt;label&gt;Account No:&lt;/label&gt;
             &lt;input type="text" name="int_custID" id="int_custID" /&gt;
             &lt;label&gt;Company:&lt;/label&gt;
             &lt;input type="text" name="cust_company" id="cust_company"/&gt;
              &lt;label&gt;Address:&lt;/label&gt;
              &lt;textarea type="text" rows=5 name="cust_address" id="cust_address"&gt;&lt;/textarea&gt;
              &lt;label&gt;Postcode:&lt;/label&gt;
              &lt;input type="text" name="cust_postcode" id="cust_postcode"/&gt;
              &lt;label&gt;Contact 1:&lt;/label&gt;
              &lt;input type="text" name="cust_contact_1" id="cust_contact_1"/&gt;
              &lt;label&gt;Contact 2:&lt;/label&gt;
              &lt;input type="text" name="cust_contact_2"  id="cust_contact_2"/&gt;
              &lt;label&gt;Telephone:&lt;/label&gt;
              &lt;input type="text" name="cust_tel" id="cust_tel"/&gt;
              &lt;label&gt;Mobile:&lt;/label&gt;
              &lt;input type="text" name="cust_mob" id="cust_mob"/&gt;
              &lt;label&gt;DDI:&lt;/label&gt;
              &lt;input type="text" name="cust_DDI" id="cust_DDI"/&gt;
              &lt;label&gt;Email:&lt;/label&gt;
              &lt;input type="email" name="cust_email" id="cust_email"/&gt;
              &lt;label&gt;Notes:&lt;/label&gt;
              &lt;textarea type="text" rows=5 colums=1 name="cust_notes" id="cust_notes"&gt;&lt;/textarea&gt;
  
             &lt;input type="submit" name="cust_upd" id="cust_upd" value="Update"&gt;
             &lt;input type="submit" name="cust_del" id="cust_del" value="Delete"&gt;
          &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>load.php contains require_once db.php, class.php &amp; config.php (contains username and password). This file is okay. </p>
  
  <p>Thanks for any help you may be able to give!</p>
  
  <p><strong>EDITTED</strong>
  Thanks for all your help! Here is the working code for anyone who needs it!</p>
  
  <pre><code>function ins_upd($table, $values) {
              try{
                  include('config.php');
                  $conn = new PDO('mysql:host=localhost;dbname=displaytrends;charset=utf8', $DB_USER, $DB_PASS);
                  $conn-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                  //Strip $_POST array to fields with values  
                  $values=array_filter($values);
                  //Take array keys from array
                  $field_keys=array_keys($values);
                  //Implode for insert fields
                  $ins_fields=implode(",", $field_keys);
                  //Implode for insert value fields (values will binded later)
                  $value_fields=":" . implode(", :", $field_keys);
                  //Create update fields for each array create value = 'value = :value'.
                  $update_fields=array_keys($values);
                  foreach($update_fields as &amp;$val){
                      $val=$val." = :".$val;
                  }
                  $update_fields=implode(", ", $update_fields);
                  //SQL Query
                  $insert = "INSERT INTO $table ($ins_fields) VALUES ($value_fields) ON DUPLICATE KEY UPDATE $update_fields";
                  $query = $conn-&gt;prepare($insert);
                  //Bind each value based on value coming in.
                  foreach ($values as $key =&gt; &amp;$value) {
                      switch(gettype($value)) {
                          case 'integer':
                          case 'double':
                          $query-&gt;bindParam(':' . $key, $value, PDO::PARAM_INT);
                          break;
                          default:
                          $query-&gt;bindParam(':' . $key, $value, PDO::PARAM_STR);
                      }
                  }
                  $query-&gt;execute();
              } catch(PDOException $e) {
                  echo 'ERROR: ' . $e-&gt;getMessage();
              }   
          }
  </code></pre>