67 lines
2.2 KiB
PHP
67 lines
2.2 KiB
PHP
<?php
|
|
include 'koneksi.php';
|
|
session_start();
|
|
|
|
if (!isset($_SESSION['username'])) {
|
|
header('Location: loginn.php');
|
|
exit;
|
|
}
|
|
|
|
$message = '';
|
|
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 = mysqli_query($conn, "UPDATE users SET balance = balance + $amount WHERE username = '$username'");
|
|
if ($update) {
|
|
// Fetch new balance
|
|
$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'];
|
|
} else {
|
|
$message = 'Top up berhasil, tetapi gagal mengambil saldo terbaru.';
|
|
}
|
|
} else {
|
|
$message = 'Gagal memproses top up. Coba lagi.';
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!doctype html>
|
|
<html lang="id">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
|
<title>Top Up - OCA GameHub</title>
|
|
<link rel="stylesheet" href="login.css" />
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h2>Top Up Saldo</h2>
|
|
<p>Pengguna: <strong><?php echo htmlspecialchars($_SESSION['username']); ?></strong></p>
|
|
<p>Saldo saat ini: <strong><?php echo isset($_SESSION['balance']) ? (int)$_SESSION['balance'] : 0; ?></strong></p>
|
|
|
|
<?php if ($message): ?>
|
|
<div class="success-message show"><?php echo htmlspecialchars($message); ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form method="POST" action="topup.php">
|
|
<div class="form-group">
|
|
<label for="amount">Jumlah Top Up (angka):</label>
|
|
<input id="amount" name="amount" type="number" min="1" required />
|
|
</div>
|
|
<div class="button-group">
|
|
<button type="submit" class="btn btn-register">Top Up</button>
|
|
<a href="html.php" class="btn btn-signin">Kembali</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</body>
|
|
</html>
|