User: = $_SESSION['username'] ?>
-Saldo Bank Sekarang: Rp = number_format($currentBank, 0, ',', '.') ?>
++ + - +
diff --git a/html.php b/html.php index 12f0f97..30dc1cb 100644 --- a/html.php +++ b/html.php @@ -2,12 +2,13 @@ session_start(); include "koneksi.php"; +// Jika tidak login, redirect ke login if (!isset($_SESSION['user_id'])) { - header("Location: loginn.php"); + header('Location: loginn.php'); exit; } -// Selalu ambil saldo dari DATABASE +// Ambil saldo dari database $query = "SELECT bank FROM users WHERE id = ?"; $stmt = mysqli_prepare($conn, $query); mysqli_stmt_bind_param($stmt, "i", $_SESSION['user_id']); @@ -17,42 +18,527 @@ $data = mysqli_fetch_assoc($result); $currentBank = intval($data["bank"]); $_SESSION["bank"] = $currentBank; -?> +$_SESSION["balance"] = $currentBank; +// Handle Top Up form +$topup_response = ''; +$topup_success = false; +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['bank_method'])) { + $bank_method = $_POST['bank_method'] ?? ''; + $amount = (int)($_POST['amount'] ?? 0); + + if (empty($bank_method)) { + $topup_response = 'Pilih metode pembayaran terlebih dahulu.'; + } elseif ($amount <= 0) { + $topup_response = 'Masukkan jumlah top up yang valid (lebih dari 0).'; + } else { + // Update saldo di database + $new_balance = $currentBank + $amount; + $update_query = "UPDATE users SET bank = ? WHERE id = ?"; + $update_stmt = mysqli_prepare($conn, $update_query); + mysqli_stmt_bind_param($update_stmt, "ii", $new_balance, $_SESSION['user_id']); + + if (mysqli_stmt_execute($update_stmt)) { + $_SESSION['balance'] = $new_balance; + $_SESSION['bank'] = $new_balance; + $currentBank = $new_balance; + + $topup_response = 'Top up berhasil! Saldo: Rp ' . number_format($_SESSION['balance'], 0, ',', '.'); + $topup_success = true; + + // Refresh halaman untuk update saldo + header("Refresh:2"); + } else { + $topup_response = 'Gagal melakukan top up. Coba lagi.'; + } + } +} + +// Handle balance update from game +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_balance'])) { + $new_balance = (int)$_POST['update_balance']; + if ($new_balance >= 0) { + // Update saldo di database + $update_query = "UPDATE users SET bank = ? WHERE id = ?"; + $update_stmt = mysqli_prepare($conn, $update_query); + mysqli_stmt_bind_param($update_stmt, "ii", $new_balance, $_SESSION['user_id']); + + if (mysqli_stmt_execute($update_stmt)) { + $_SESSION['balance'] = $new_balance; + $_SESSION['bank'] = $new_balance; + header('Content-Type: application/json'); + echo json_encode(['status' => 'success', 'balance' => $_SESSION['balance']]); + exit; + } else { + header('Content-Type: application/json'); + echo json_encode(['status' => 'error', 'message' => 'Failed to update balance']); + exit; + } + } +} +?>
- - -User: = $_SESSION['username'] ?>
-Saldo Bank Sekarang: Rp = number_format($currentBank, 0, ',', '.') ?>
+