Summary Files Commits Branches @commit @diff @stats
path: root / src / models
diff options
context:
space:
mode:
2 files changed, 42 insertions, 7 deletions
-rw-r--r--src/models/DB.php42
-rw-r--r--src/models/JobOffer.php7
diff --git a/src/models/DB.php b/src/models/DB.php
index 4c75d17..b287273 100644
--- a/src/models/DB.php
+++ b/src/models/DB.php
@@ -12,8 +12,13 @@ class DB {
private PDO $connection;
function __construct() {
- $this->connection = new PDO("mysql:host=" . self::$servername, self::$username, self::$password);
- $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ try {
+ $this->connection = new PDO("mysql:host=" . self::$servername, self::$username, self::$password);
+ $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
// Make sure the actual db exists
$createDBQuery = "CREATE DATABASE IF NOT EXISTS " . self::$dbname . ";";
@@ -36,7 +41,12 @@ class DB {
$query .= "PRIMARY KEY (id));";
- $this->connection->exec($query);
+ try {
+ $this->connection->exec($query);
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
}
function insertValue(string $tableName, ...$values) {
@@ -54,7 +64,12 @@ class DB {
$query .= ");";
- $this->connection->exec($query);
+ try {
+ $this->connection->exec($query);
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
}
/* Read */
@@ -84,8 +99,23 @@ class DB {
$query = rtrim($query, ", ");
$query .= " WHERE id = {$id};";
- echo $query;
- $this->connection->exec($query);
+ try {
+ $this->connection->exec($query);
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
+ }
+
+ /* Delete */
+
+ function deleteById(string $tableName, int $id) {
+ try {
+ $this->connection->exec("DELETE FROM {$tableName} WHERE id = {$id}");
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
}
}
diff --git a/src/models/JobOffer.php b/src/models/JobOffer.php
index 2627207..5d89048 100644
--- a/src/models/JobOffer.php
+++ b/src/models/JobOffer.php
@@ -50,7 +50,12 @@ class JobOffer extends DBModel {
* @param JobOffer $jb
*/
static function updateFromDB($jb) {
- echo "here";
self::$DB->updateById(self::TABLE_NAME, $jb->id, "title", $jb->title, "description", $jb->description, "company", $jb->company, "salary", $jb->salary);
}
+
+ /* Delete */
+
+ static function deleteFromDB(int $id) {
+ self::$DB->deleteById(self::TABLE_NAME, $id);
+ }
}