title: create PHP function for PDO insert query
body: <p>I have tried to create a function for an SQL/PDO Insert query:</p>
  
  <pre><code>function InsertQuery ($table,$cols,$values) {
      global $pdo_conn;
  
      foreach($values as $values2) {
          $values2 = $values2;
      }
      $stmt='INSERT into $table (';
      foreach($cols as $cols2) {
          $stmt.=" ".$cols2.", ";
      }
      $stmt.=" ) VALUES ( ";
      foreach($cols as $cols2) {
          $stmt.=" :".$cols2." ";
      }
      $stmt.=" ) ";
  
      $stmt2 = $pdo_conn-&gt;prepare($stmt);
      foreach($cols as $cols2) {
      $stmt2-&gt;bindParam(':$cols2', $cols2);
      }
  }
  </code></pre>
  
  <p>but i am getting the error:</p>
  
  <blockquote>
    <p>Catchable fatal error: Object of class PDOStatement could not be converted to string in /home/integra/public_html/admin/includes/functions.php on line 30</p>
  </blockquote>
  
  <p>please be patient with me as i am new to PDO and just used to using MySQL</p>
  
  <p>have i put the prepared statement wrong or my foreach loops?</p>
  
  <p>I believe the statement should look like:</p>
  
  <pre><code>$stmt2 = $pdo_conn-&gt;prepare('INSERT into $table (col1) values (:val1)');
  $stmt2-&gt;bindParam(':$val1', $val);
  </code></pre>
  
  <p>here is how i called my function:</p>
  
  <pre><code>$col=array('col1');
  $val=array('val1');
  InsertQuery ("table1",$col,$val);
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Ok here is my new code:</p>
  
  <pre><code>global $pdo_conn;
  foreach($values as $values2) {
      $values2 = $values2;
  }
  $stmt='INSERT into '.$table.' (';
  foreach($cols as $cols2) {
      $stmt.=" ".implode(",", $cols2)." ";
  }
  $stmt.=" ) VALUES ( ";
  foreach($cols as $cols2) {
      $stmt.=" :".implode(",", $cols2)." ";
  }
  $stmt.=" ) ";
  
  $stmt2 = $pdo_conn-&gt;prepare($stmt);
  foreach($cols as $cols2) {
      $stmt2-&gt;bindParam(':$cols2', $cols2);
  }
  </code></pre>
  
  <p>but i now get the error about the implode:</p>
  
  <blockquote>
    <p>Warning: implode() [function.implode]: Invalid arguments passed in /home/integra/public_html/admin/includes/functions.php on line 18</p>
    
    <p>Warning: implode() [function.implode]: Invalid arguments passed in /home/integra/public_html/admin/includes/functions.php on line 22</p>
  </blockquote>
  
  <p>which i think is because there is nothing to implode as there is only one column and one value</p>