Esempio n. 1
0
 /**
  * Inserts a record into the database using a series of insert columns
  * and corresponding insertvalues. Returns the insert id.
  *
  * @param string $table			  table to perform query on
  * @param array  $insertcolumns columns to be inserted
  * @param array  $insertvalues  values to be inserted
  *
  * @return integer $insertid	  insert id from driver, new record id
  */
 protected function insertRecord($table, $insertcolumns, $insertvalues)
 {
     $default = $this->defaultValue;
     $idfield = $this->safeColumn($this->getIDField($table));
     $suffix = $this->getInsertSuffix($table);
     $table = $this->safeTable($table);
     if (count($insertvalues) > 0 && is_array($insertvalues[0]) && count($insertvalues[0]) > 0) {
         foreach ($insertcolumns as $k => $v) {
             $insertcolumns[$k] = $this->safeColumn($v);
         }
         $insertSQL = "INSERT INTO {$table} ( {$idfield}, " . implode(",", $insertcolumns) . " ) VALUES ";
         $insertSQL .= "( {$default}, " . implode(",", array_fill(0, count($insertcolumns), " ? ")) . " ) {$suffix}";
         foreach ($insertvalues as $i => $insertvalue) {
             $ids[] = $this->adapter->getCell($insertSQL, $insertvalue, $i);
         }
         $result = count($ids) === 1 ? array_pop($ids) : $ids;
     } else {
         $result = $this->adapter->getCell("INSERT INTO {$table} ({$idfield}) VALUES({$default}) {$suffix}");
     }
     if ($suffix) {
         return $result;
     }
     $last_id = $this->adapter->getInsertID();
     return $this->adapter->getErrorMsg() == "" ? $last_id : 0;
 }
Esempio n. 2
0
 /**
  * Inserts a record into the database using a series of insert columns
  * and corresponding insertvalues. Returns the insert id.
  * @param string $table
  * @param array $insertcolumns
  * @param array $insertvalues
  * @return integer $insertid
  */
 public function insertRecord($table, $insertcolumns, $insertvalues)
 {
     //if ($table == "__log") $idfield="id"; else
     $idfield = $this->getIDField($table);
     $table = $this->check($table);
     if (count($insertvalues) > 0 && is_array($insertvalues[0]) && count($insertvalues[0]) > 0) {
         foreach ($insertcolumns as $k => $v) {
             $insertcolumns[$k] = "`" . $this->check($v) . "`";
         }
         $insertSQL = "INSERT INTO `{$table}` ( {$idfield}, " . implode(",", $insertcolumns) . " ) VALUES ";
         $pat = "( NULL, " . implode(",", array_fill(0, count($insertcolumns), " ? ")) . " )";
         $insertSQL .= implode(",", array_fill(0, count($insertvalues), $pat));
         foreach ($insertvalues as $insertvalue) {
             foreach ($insertvalue as $v) {
                 $vs[] = strval($v);
             }
         }
         $this->adapter->exec($insertSQL, $vs);
         return $this->adapter->getErrorMsg() == "" ? $this->adapter->getInsertID() : 0;
     } else {
         $this->adapter->exec("INSERT INTO `{$table}` ({$idfield}) VALUES(NULL) ");
         return $this->adapter->getErrorMsg() == "" ? $this->adapter->getInsertID() : 0;
     }
 }