function dbsave() { $db = new ConexionBd(); if (!empty($this->id)) { $update = "UPDATE `" . $this->tabla . "` SET "; $arrayCampos = array(); foreach ($this->campos as $c => $nombre) { $arrayCampos[] = "`" . $c . "` = '" . $this->{$c} . "'"; } $update .= implode(', ', $arrayCampos); $update .= " WHERE id = '" . $this->id . "'"; $db->query($update); return $this->id; } else { $insert = "INSERT INTO `" . $this->tabla . "`\n\t\t\t\t\t\t\tVALUES("; $arrayCampos = array(); foreach ($this->campos as $c => $nombre) { if ($c != 'id') { $arrayCampos[] = "'" . $this->{$c} . "'"; } else { $arrayCampos[] = '0'; } } $insert .= implode(', ', $arrayCampos); $insert .= ");"; $db->query($insert); return $db->insertId(); } }