diff options
1 files changed, 30 insertions, 10 deletions
| -rw-r--r-- | src/models/DB.php | 40 |
diff --git a/src/models/DB.php b/src/models/DB.php index b973e4b..4c75d17 100644 --- a/src/models/DB.php +++ b/src/models/DB.php @@ -24,6 +24,8 @@ class DB { $this->connection->exec("USE " . self::$dbname); } + /* Create */ + function createTable(string $tableName, array $columns) { $query = "CREATE TABLE IF NOT EXISTS {$tableName}( id INT NOT NULL AUTO_INCREMENT,"; @@ -37,6 +39,26 @@ class DB { $this->connection->exec($query); } + function insertValue(string $tableName, ...$values) { + $query = "INSERT INTO {$tableName} VALUES(NULL, "; + + foreach($values as $value) { + if (gettype($value) == "string") { + $query .= "'{$value}', "; + } + else { + $query .= "{$value}, "; + } + } + $query = rtrim($query, ", "); + + $query .= ");"; + + $this->connection->exec($query); + } + + /* Read */ + function getById(string $tableName, int $id) { $sth = $this->connection->prepare("SELECT * FROM `{$tableName}` WHERE id = {$id}"); $sth->execute(); @@ -51,20 +73,18 @@ class DB { return $sth->fetchAll(PDO::FETCH_OBJ); } - function insertValue(string $tableName, ...$values) { - $query = "INSERT INTO {$tableName} VALUES(NULL, "; + /* Update */ - foreach($values as $value) { - if (gettype($value) == "string") { - $query .= "'{$value}', "; - } - else { - $query .= "{$value}, "; - } + function updateById(string $tableName, int $id, ...$values) { + $query = "UPDATE {$tableName} SET "; + + for ($i = 0; $i < sizeof($values); $i += 2) { + $query .= "{$values[$i]} = " . (gettype($values[$i+1]) == "string" ? "'{$values[$i+1]}'" : $values[$i+1]) . ", "; } $query = rtrim($query, ", "); - $query .= ");"; + $query .= " WHERE id = {$id};"; + echo $query; $this->connection->exec($query); } |
