Advertisement
michaelyuen

Untitled

Oct 8th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.66 KB | None | 0 0
  1. <?php
  2. ini_set('display_errors',1); // enable php error display for easy trouble shooting
  3. error_reporting(E_ALL); // set error display to all
  4.  
  5. function db_insert($table, $array = array()) {
  6.     global $con;
  7.  
  8.     foreach ($array as $field => $value) {
  9.         $fields[] = $field;
  10.         $values[] = $con->real_escape_string($value);
  11.     }
  12.    
  13.     $sql = "INSERT INTO {$table} (`" . implode("`,`", $fields) , "`) VALUES ('" . implode("','", $values) . "')";
  14.     if ($con->query($sql)) {
  15.         return true;
  16.     }
  17.     return false;
  18. }
  19.  
  20. // HOW TO USE:
  21.  
  22. $array = array(
  23.                     'column1' => 'value1',
  24.                     'column2' => 123,
  25.                     'column3' => 'value3'
  26.                     )
  27.                    
  28.  
  29. insert('table_name', $array);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement