Return to Snippet

Revision: 32335
at September 22, 2010 21:02 by shawntysco


Initial Code
//Insert
//$sql = "INSERT INTO quick_links (title, link) VALUES('" . $_POST['title'] . "', '" . $link . "');";
function add($table, $columns, $values){

  //begin the insert into*******************************************************
  $sql = "INSERT INTO " . $table;

  //iterate through the $table**************************************************
  
  //iterate through the $columns ***********************************************
  $sql = $sql . " (" . $columns . ") VALUES(";
  
  //iterate through the $values*************************************************
  $arrValue = explode("|", $values);
   //loop the array
   foreach($arrValue as $arrValueResult){
   
    //check if last index in array
          if (end($arrValue) == $arrValueResult){
            if((is_numeric($arrValueResult)) || ($arrValueResult == "NOW()")){
                 $arrValueResult = $arrValueResult ;

              }else{
                 $arrValueResult = "'" . $arrValueResult . "'" ;
              }
          }else{
              if((is_numeric($arrValueResult)) || ($arrValueResult == "NOW()")){
                 $arrValueResult = $arrValueResult . ", " ;
              }else{
                 $arrValueResult = "'" . $arrValueResult . "', " ;
              }
          }//end if
      
      //add to $sql
      $sql = $sql . $arrValueResult;
   }//end for each
   
   //end the $sql
   $sql = $sql . ");";
  
  //determine is item in $values is an interger / string
  //echo $sql;
  //insert the query
  $insert_sql = querys($sql);
}//end add

//-----------------------------------------------------------------
//To use this function:
$add_cat = add("categories", "cat", $_POST['cat']);

Initial URL
linkdeo.net

Initial Description
Makes basic insert statements a breeze to type while doing basic PHP inserts.\r\n*The function for \\\'querys\\\' on line 44 can be found here: http://snipplr.com/view/40911/query-the-database-php/   *

Initial Title
PHP, Build a basic dynamic  INSERT string

Initial Tags
mysql, php

Initial Language
PHP