public function insert() { global $g_item_fields; if ($this->id) { trigger_error("User object which already has an id, can't be inserted", E_USER_WARNING); } if (!sizeof($this->field_states)) { trigger_error("need members to update to database. e.g. 'firstname,lastname,data'", E_USER_WARNING); } $dbh = new DB_Mysql(); $t_fields = array(); $t_values = array(); foreach ($g_item_fields as $f) { $name = $f->name; if (!isset($this->{$name}) && $this->{$name} != NULL) { trigger_error("{$name} is not a member of {$this} and can't be passed to db", E_USER_WARNING); } $t_fields[] = $name; $t_values[] = "'" . asSecureString($this->{$name}) . "'"; } $prefix = confGet('DB_TABLE_PREFIX'); $str_query = 'INSERT INTO ' . $prefix . 'item ' . '(' . join(', ', $t_fields) . ')' . ' VALUES(' . join(', ', $t_values) . ')'; $sth = $dbh->prepare($str_query); $sth->execute("", 1); #--- extract the id of last inserted item --- $this->id = $dbh->lastId(); $dbh = new DB_Mysql(); $t_fields = array(); $t_values = array(); foreach ($this->fields as $f) { $name = $f->name; ### skip project-item fields ### if (isset($this->fields[$name]) && isset($this->fields[$name]->in_db_object) || !isset($g_item_fields[$name])) { if (!isset($this->{$name}) && $this->{$name} != NULL) { trigger_error("{$name} is not a member of {$this} and can't be passed to db", E_USER_WARNING); } $t_fields[] = $name; $t_values[] = "'" . asSecureString($this->{$name}) . "'"; } } $str_query = 'INSERT INTO ' . $prefix . $this->_type . '(' . join(',', $t_fields) . ')' . ' VALUES(' . join(',', $t_values) . ')'; $sth = $dbh->prepare($str_query); $sth->execute("", 1); return true; }