Files
2026-09-16 23:20:08 +07:00

57 lines
2.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
require_once '../includes/db.php';
try {
// Create admin_users table
$pdo->exec("CREATE TABLE IF NOT EXISTS admin_users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
cid VARCHAR(13) NULL,
password_hash VARCHAR(255) NOT NULL,
role VARCHAR(20) DEFAULT 'admin',
google2fa_secret VARCHAR(255) NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)");
// Ensure columns exist (in case of old table structure)
try { $pdo->exec("ALTER TABLE admin_users ADD COLUMN cid VARCHAR(13) NULL"); } catch (Exception $e) {}
try { $pdo->exec("ALTER TABLE admin_users ADD COLUMN role VARCHAR(20) DEFAULT 'admin'"); } catch (Exception $e) {}
// Force update superadmin role just in case
$pdo->exec("UPDATE admin_users SET role = 'superadmin' WHERE username = 'superadmin'");
// Create settings table
$pdo->exec("CREATE TABLE IF NOT EXISTS settings (
setting_key VARCHAR(50) PRIMARY KEY,
setting_value VARCHAR(255)
)");
$pdo->exec("INSERT IGNORE INTO settings (setting_key, setting_value) VALUES ('require_assessment', '0')");
// Check if superadmin exists
$stmt = $pdo->prepare("SELECT id FROM admin_users WHERE username = 'superadmin'");
$stmt->execute();
if (!$stmt->fetch()) {
$defaultPass = password_hash('superadmin123', PASSWORD_DEFAULT);
$pdo->exec("INSERT INTO admin_users (username, password_hash, role) VALUES ('superadmin', '$defaultPass', 'superadmin')");
echo "✅ สร้างบัญชี Super Admin สำเร็จ (Username: superadmin / Password: superadmin123)<br>";
} else {
echo "️ บัญชี Super Admin มีอยู่แล้ว<br>";
}
echo "✅ ตาราง admin_users อัปเดตเรียบร้อย<br>";
// Debug: Show all users
echo "<hr><h3>ตรวจสอบรายชื่อ Admin ในฐานข้อมูล:</h3>";
$stmt = $pdo->query("SELECT id, username, role FROM admin_users");
while ($row = $stmt->fetch()) {
echo "ID: {$row['id']} | Username: {$row['username']} | Role: {$row['role']}<br>";
}
echo "<br><a href='login.php'>ไปที่หน้าเข้าสู่ระบบ</a>";
} catch (\PDOException $e) {
die("Error updating database: " . $e->getMessage());
}
?>