From 9052e24b7dfc87e1d9500ea1a8241391b089e0fc Mon Sep 17 00:00:00 2001 From: Cliff Date: Mon, 1 Dec 2025 11:04:47 +0700 Subject: [PATCH] repair topup --- topup.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/topup.php b/topup.php index 8ce1c69..af183d1 100644 --- a/topup.php +++ b/topup.php @@ -8,21 +8,29 @@ if (!isset($_SESSION['username'])) { } $message = ''; +$username = mysqli_real_escape_string($conn, $_SESSION['username']); + +// Load balance from database setiap kali halaman dibuka +$res = mysqli_query($conn, "SELECT balance FROM users WHERE username = '$username'"); +if ($res && mysqli_num_rows($res) > 0) { + $row = mysqli_fetch_assoc($res); + $_SESSION['balance'] = (int)$row['balance']; +} + if ($_SERVER['REQUEST_METHOD'] === 'POST') { $amount = isset($_POST['amount']) ? (int)$_POST['amount'] : 0; if ($amount <= 0) { $message = 'Masukkan jumlah top up yang valid (lebih dari 0).'; } else { - $username = mysqli_real_escape_string($conn, $_SESSION['username']); - // Update balance in DB + // Update balance di database $update = mysqli_query($conn, "UPDATE users SET balance = balance + $amount WHERE username = '$username'"); if ($update) { - // Fetch new balance + // Fetch saldo terbaru $res = mysqli_query($conn, "SELECT balance FROM users WHERE username = '$username'"); if ($res && mysqli_num_rows($res) > 0) { $row = mysqli_fetch_assoc($res); $_SESSION['balance'] = (int)$row['balance']; - $message = 'Top up berhasil. Saldo sekarang: ' . $_SESSION['balance']; + $message = 'Top up berhasil! Saldo sekarang: Rp ' . number_format($_SESSION['balance'], 0, ',', '.'); } else { $message = 'Top up berhasil, tetapi gagal mengambil saldo terbaru.'; }