From 0dcdfa7e76e8e022a8327b2fea090108a4857692 Mon Sep 17 00:00:00 2001 From: max Date: Tue, 15 Apr 2025 20:32:50 +0200 Subject: [PATCH] update to init db if empty --- db_connection.php | 50 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/db_connection.php b/db_connection.php index 462aa68..51c55e1 100644 --- a/db_connection.php +++ b/db_connection.php @@ -1,16 +1,60 @@ setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + // Check if the User table exists + $result = $db->query("SELECT name FROM sqlite_master WHERE type='table' AND name='User'"); + $userTableExists = $result->fetchColumn(); + + if (!$userTableExists) { + // Initialize the database structure + $db->exec(" + CREATE TABLE User ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + vorname TEXT NOT NULL, + nachname TEXT NOT NULL, + username TEXT UNIQUE NOT NULL, + password TEXT NOT NULL + ) + "); + + $db->exec(" + CREATE TABLE Restaurant ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + maps_link TEXT + ) + "); + + $db->exec(" + CREATE TABLE Bewertung ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + fk_user_id INTEGER NOT NULL, + fk_restaurant_id INTEGER NOT NULL, + bewertung INTEGER NOT NULL CHECK (bewertung BETWEEN 0 AND 10), + bewertung_str TEXT, + FOREIGN KEY (fk_user_id) REFERENCES User(id), + FOREIGN KEY (fk_restaurant_id) REFERENCES Restaurant(id) + ) + "); + + echo "

Database initialized successfully!

"; + } else { + // Optionally, you can add a message indicating the database already exists + // echo "

Database already exists.

"; + } + } catch (PDOException $e) { die("Database connection failed: " . $e->getMessage()); } -// Function to sanitize user input (remains the same) function sanitize_input($data) { $data = trim($data); $data = stripslashes($data); - return htmlspecialchars($data); + $data = htmlspecialchars($data); + return $data; } +?> \ No newline at end of file