repair topup
This commit is contained in:
parent
e040aa20d7
commit
9052e24b7d
16
topup.php
16
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.';
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user