35418-vm/db/setup.php
2025-11-02 14:43:46 +00:00

35 lines
1.2 KiB
PHP

<?php
require_once 'config.php';
try {
$pdo = db();
$sql = "
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
user_role ENUM('learner', 'admin') NOT NULL DEFAULT 'learner',
level INT DEFAULT 1,
xp INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);";
$pdo->exec($sql);
// Optional: Create a default admin user if one doesn't exist
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = 'admin'");
$stmt->execute();
if ($stmt->rowCount() == 0) {
$admin_pass = 'admin123'; // Super secure default password
$admin_hash = password_hash($admin_pass, PASSWORD_DEFAULT);
$admin_sql = "INSERT INTO users (username, password_hash, user_role) VALUES ('admin', ?, 'admin')";
$admin_stmt = $pdo->prepare($admin_sql);
$admin_stmt->execute([$admin_hash]);
echo "Default admin user created with username 'admin' and password 'admin123'.<br>";
}
echo "Database setup completed successfully!";
} catch (PDOException $e) {
die("Database setup failed: " . $e->getMessage());
}
?>