Insert to MySQL PDO Function

Below is a PDO insert function that I wrote to save me time from
constantly constructing the insert query whenever I need it. Now I
simply pass my values and fields to the function and all is taken care
of.

<?php
//START: SQLInsert
//$fields = array of fields in DB
//$values = array of values respective to the $fields
if (!function_exists("SQLInsert")) : function
SQLInsert($table,$fields,$values) {
 
  //Connect to DB
  include('connect.php');
 
  //build the fields
  $buildFields = '';
  if (is_array($fields)) {
 
    //loop through all the fields
    foreach($fields as $key => $field) :
      if ($key == 0) {
        //first item
        $buildFields .= $field;
      } else {
        //every other item follows with a ","
        $buildFields .= ', '.$field;
      }
    endforeach;
 
  } else {
    //we are only inserting one field
    $buildFields .= $fields;
  }
 
  //build the values
  $buildValues = '';
  if (is_array($values)) {
 
    //loop through all the values
    foreach($values as $key => $value) :
      if ($key == 0) {
        //first item
        $buildValues .= '?';
      } else {
        //every other item follows with a ","
        $buildValues .= ', ?';
      }
    endforeach;
 
  } else {
    //we are only inserting one field
    $buildValues .= ':value';
  }
 
  $prepareInsert = $db->prepare('INSERT INTO '.$table.' ('.$buildFields.') VALUES ('.$buildValues.')');
 
  //execute the update for one or many values
  if (is_array($values)) {
    $prepareInsert->execute($values);
  } else {
    $prepareInsert->execute(array(':value' => $values));
  }
 
  //record and print any DB error that may be given
  $error = $prepareInsert->errorInfo();
  if ($error[1]) print_r($error);
 
} endif;
//END: SQLInsert
?>

Here are some examples of how to use it:

<?php
//inserting one item
SQLInsert('db_items', 'db_field', 'value');
 
//inserting multiple items
$fields[] = 'name';
$fields[] = 'description';
$fields[] = 'value';
$fields[] = 'stock';
 
$values[] = $_POST['name'];
$values[] = $_POST['description'];
$values[] = $_POST['value'];
$values[] = $_POST['stock'];
 
SQLInsert('db_items', $fields, $values);
?>

Comments

Thank you this seems nice,I will use it:)

you code is well organized.  but, i don't know why you have to use some of the operators and whatnot the way that you do.

Bad way

If you wouldn't mind, please let me know what flaws you see or even how you would go about it. I did this for as a pretty wide open way of updating any sort of variable type. PDO will automatically assign it to a string type. I was thinking of adding a thrid function property of "type" and have the user define the type they were inserting. Please let me know your thoughts. dustin [@] digipiph.com

Thanks!

~dc

Exactly what I was looking for. No idea why one of the comments says "bad" - it seems exactly the right way to do it. Thanks!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
If you have questions about anything on this site, you can find me on Google+: