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 = '';
|
$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') {
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
$amount = isset($_POST['amount']) ? (int)$_POST['amount'] : 0;
|
$amount = isset($_POST['amount']) ? (int)$_POST['amount'] : 0;
|
||||||
if ($amount <= 0) {
|
if ($amount <= 0) {
|
||||||
$message = 'Masukkan jumlah top up yang valid (lebih dari 0).';
|
$message = 'Masukkan jumlah top up yang valid (lebih dari 0).';
|
||||||
} else {
|
} else {
|
||||||
$username = mysqli_real_escape_string($conn, $_SESSION['username']);
|
// Update balance di database
|
||||||
// Update balance in DB
|
|
||||||
$update = mysqli_query($conn, "UPDATE users SET balance = balance + $amount WHERE username = '$username'");
|
$update = mysqli_query($conn, "UPDATE users SET balance = balance + $amount WHERE username = '$username'");
|
||||||
if ($update) {
|
if ($update) {
|
||||||
// Fetch new balance
|
// Fetch saldo terbaru
|
||||||
$res = mysqli_query($conn, "SELECT balance FROM users WHERE username = '$username'");
|
$res = mysqli_query($conn, "SELECT balance FROM users WHERE username = '$username'");
|
||||||
if ($res && mysqli_num_rows($res) > 0) {
|
if ($res && mysqli_num_rows($res) > 0) {
|
||||||
$row = mysqli_fetch_assoc($res);
|
$row = mysqli_fetch_assoc($res);
|
||||||
$_SESSION['balance'] = (int)$row['balance'];
|
$_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 {
|
} else {
|
||||||
$message = 'Top up berhasil, tetapi gagal mengambil saldo terbaru.';
|
$message = 'Top up berhasil, tetapi gagal mengambil saldo terbaru.';
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user