From 0275422dd033cde148856eb6025ef91bd2d193fb Mon Sep 17 00:00:00 2001 From: aldo Date: Tue, 16 Dec 2025 00:01:08 +0700 Subject: [PATCH] perbaikan --- db.php | 7 ++--- save_score.php | 2 +- setup.php | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 setup.php diff --git a/db.php b/db.php index 2d90421..31ae715 100644 --- a/db.php +++ b/db.php @@ -1,8 +1,8 @@ getMessage(), (int)$e->getCode()); diff --git a/save_score.php b/save_score.php index 905518f..e8eecd8 100644 --- a/save_score.php +++ b/save_score.php @@ -12,7 +12,7 @@ if (!isset($_POST['difficulty'], $_POST['time'])) { exit; } -$username ='testuser'; +$username = $_SESSION['username']; $difficulty = $_POST['difficulty']; $time = (int) $_POST['time']; diff --git a/setup.php b/setup.php new file mode 100644 index 0000000..41da8ea --- /dev/null +++ b/setup.php @@ -0,0 +1,74 @@ +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.
"; +} 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.
"; + + // 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'.
"; + } + +} catch (PDOException $e) { + echo "Error Creating 'users': " . $e->getMessage() . "
"; +} + +// 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.
"; + +} catch (PDOException $e) { + echo "Error Creating 'leaderboard_sudoku': " . $e->getMessage() . "
"; +} + +echo "
Setup Complete! You can now use the application."; +?>