update to init db if empty

This commit is contained in:
max 2025-04-15 20:32:50 +02:00
parent 7cd04e2a52
commit 0dcdfa7e76

View File

@ -1,16 +1,60 @@
<?php
$databaseFile = 'rippchen.db'; // Path to your SQLite database file
$databaseFile = 'mydatabase.db';
try {
$db = new PDO("sqlite:" . $databaseFile);
$db->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 "<p style='color:green;'>Database initialized successfully!</p>";
} else {
// Optionally, you can add a message indicating the database already exists
// echo "<p style='color:blue;'>Database already exists.</p>";
}
} 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;
}
?>