public function insert($req) { try { $sql = 'INSERT INTO ' . $this->table . ' '; $variable = '( '; $valeur = '( '; foreach ($req as $k => $v) { $variable .= $k . ','; $valeur .= '"' . urlencode($v) . '",'; //print_r($valeur); } //die(''); $variable = trim($variable, ','); $variable .= ')'; $valeur = trim($valeur, ','); $valeur .= ');'; $sql .= $variable . ' VALUES ' . $valeur; //die($sql); @($insert = $this->db->exec($sql)); if (!$insert) { $_SESSION["ErrorInsert"] = true; } Model::$id = $this->db->lastInsertId(); } catch (PDOException $e) { echo $e->getMessage(); } Model::$connections[$this->conf] = null; }
/** * Insert a record in the database * * @param $object Model object representing data to insert */ protected function insert(Model $object) { $data = $this->escape_assoc($object->to_array()); $fields = $this->table_fields(); //generate query $query = "insert into {$this->table_name()} ( "; foreach ($fields as $field) { $query .= "{$field}, "; } //remove the last pesky comma $query = chop($query); $query = substr($query, 0, strlen($query) - 1); $query .= ' ) values ( '; foreach ($fields as $field) { $query .= "'{$data[$field]}', "; } //remove the last pesky comma, again $query = chop($query); $query = substr($query, 0, strlen($query) - 1); $query .= ' )'; $this->query($query); $object->id($this->insert_id()); return $object; }
/** * Return model id for routing/URLs * * @static * @param Model $model * @return string */ public static function model_id(Model $model) { return URL::title($model->id() . ' ' . $model->slug(), '-', true); }