<?php
require_once "header_menu.php";
$cat = $_GET['cat'] ?? '';
if(isset($_POST['buy']) && isset($_POST['id'])){
$vehicle_id = (int)$_POST['id'];
$player = $_SESSION['player_id'];
$stmt = $pdo->prepare("SELECT price FROM vehicles WHERE id=?");
$stmt->execute([$vehicle_id]);
$v = $stmt->fetch();
if($v){
$price = $v['price'];
$money = $pdo->query("SELECT money FROM players WHERE id=$player")->fetchColumn();
if($money >= $price){
$pdo->prepare("UPDATE players SET money=money-? WHERE id=?")->execute([$price,$player]);
$pdo->prepare("INSERT INTO player_vehicles (owner_id, vehicle_id, mileage) VALUES (?,?,0)")
->execute([$player,$vehicle_id]);
}
}
}
$stmt = $pdo->prepare("SELECT * FROM vehicles WHERE category=?");
$stmt->execute([$cat]);
$vehicles = $stmt->fetchAll();
?>
<h2><?=$cat?></h2>
<table>
<tr><th>Zdjęcie</th><th>Nazwa</th><th>Cena</th><th></th></tr>
<?php foreach($vehicles as $v): ?>
<tr>
<td><img src="../uploads/vehicles/<?= htmlspecialchars($v['image']) ?>" width="90"></td>
<td><?= htmlspecialchars($v['name']) ?></td>
<td><?= $v['price'] ?></td>
<td>
<form method="post">
<input type="hidden" name="id" value="<?= $v['id'] ?>">
<button name="buy">Kup</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php require_once "footer.php"; ?>
To embed this project on your website, copy the following code and paste it into your website's HTML: