37650-vm/db/setup_users.php
Flatlogic Bot 9c65e16259 sad
2026-01-21 14:05:59 +00:00

60 lines
2.0 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(255) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
role ENUM('user', 'admin') NOT NULL DEFAULT 'user',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
";
$pdo->exec($sql);
echo "Table 'users' created successfully." . PHP_EOL;
// Add or update the admin user
$username = 'admin';
$email = 'admin@gmail.com';
$password = '12345678';
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$role = 'admin';
// Check if admin already exists by email
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = :email LIMIT 1");
$stmt->execute([':email' => $email]);
$admin = $stmt->fetch();
if ($admin) {
// Update the existing admin
$update_sql = "UPDATE users SET username = :username, password_hash = :password_hash, role = :role WHERE id = :id";
$update_stmt = $pdo->prepare($update_sql);
$update_stmt->execute([
':username' => $username,
':password_hash' => $password_hash,
':role' => $role,
':id' => $admin['id']
]);
echo "Admin user updated. Username: '$username', Email: '$email'." . PHP_EOL;
} else {
// User does not exist, insert new admin user
$insert_sql = "
INSERT INTO users (username, email, password_hash, role)
VALUES (:username, :email, :password_hash, :role);
";
$insert_stmt = $pdo->prepare($insert_sql);
$insert_stmt->execute([
':username' => $username,
':email' => $email,
':password_hash' => $password_hash,
':role' => $role
]);
echo "Default admin user created. Username: '$username', Email: '$email' (Password: $password)." . PHP_EOL;
}
} catch (PDOException $e) {
die("DB ERROR: " . $e->getMessage());
}