perbaikan

This commit is contained in:
aldo 2025-12-16 00:01:08 +07:00
parent 4f2f7ae38e
commit 0275422dd0
3 changed files with 78 additions and 5 deletions

7
db.php
View File

@ -1,8 +1,8 @@
<?php <?php
$host = 'localhost'; $host = 'localhost';
$db = 'sudoku'; // Sesuaikan dengan nama database Anda $db = 'sudoku';
$user = 'root'; // Default user XAMPP $user = 'root';
$pass = ''; // Default password XAMPP (kosong) $pass = '';
$charset = 'utf8mb4'; $charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
@ -13,7 +13,6 @@ $options = [
]; ];
try { try {
// Membuat koneksi PDO
$conn = new PDO($dsn, $user, $pass, $options); $conn = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) { } catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode()); throw new \PDOException($e->getMessage(), (int)$e->getCode());

View File

@ -12,7 +12,7 @@ if (!isset($_POST['difficulty'], $_POST['time'])) {
exit; exit;
} }
$username ='testuser'; $username = $_SESSION['username'];
$difficulty = $_POST['difficulty']; $difficulty = $_POST['difficulty'];
$time = (int) $_POST['time']; $time = (int) $_POST['time'];

74
setup.php Normal file
View File

@ -0,0 +1,74 @@
<?php
$host = 'localhost';
$user = 'root';
$pass = ''; // Default XAMPP password
$charset = 'utf8mb4';
// 1. Connect to MySQL server (without DB) to create database
try {
$pdo = new PDO("mysql:host=$host;charset=$charset", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Create Database if not exists
$pdo->exec("CREATE DATABASE IF NOT EXISTS sudoku CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
echo "Database 'sudoku' successfully checked/created.<br>";
} catch (PDOException $e) {
die("DB Connection failed: " . $e->getMessage());
}
// 2. Connect to the specific Database 'sudoku'
try {
$dsn = "mysql:host=$host;dbname=sudoku;charset=$charset";
$conn = new PDO($dsn, $user, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Connection to 'sudoku' failed: " . $e->getMessage());
}
// 3. Create 'users' table
try {
$sqlUsers = "
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$conn->exec($sqlUsers);
echo "Table 'users' successfully checked/created.<br>";
// Seed default users if table is empty
$check = $conn->query("SELECT count(*) FROM users")->fetchColumn();
if ($check == 0) {
$password = password_hash('123456', PASSWORD_DEFAULT); // Default password
$sqlInsert = "INSERT INTO users (username, password) VALUES
('admin', '$password'),
('player1', '$password')";
$conn->exec($sqlInsert);
echo "Default users (admin, player1) created with password '123456'.<br>";
}
} catch (PDOException $e) {
echo "Error Creating 'users': " . $e->getMessage() . "<br>";
}
// 4. Create 'leaderboard_sudoku' table
try {
$sqlLeaderboard = "
CREATE TABLE IF NOT EXISTS leaderboard_sudoku (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50),
difficulty VARCHAR(10),
time_seconds INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)";
$conn->exec($sqlLeaderboard);
echo "Table 'leaderboard_sudoku' successfully checked/created.<br>";
} catch (PDOException $e) {
echo "Error Creating 'leaderboard_sudoku': " . $e->getMessage() . "<br>";
}
echo "<hr><strong>Setup Complete!</strong> You can now use the application.";
?>