prepare("UPDATE cars SET view_count = view_count + 1 WHERE id = ?")->execute([$id]); $stmt = $pdo->prepare("SELECT * FROM cars WHERE id = ?"); $stmt->execute([$id]); $car = $stmt->fetch(); if (!$car) { header('Location: ' . APP_URL . 'cars.php'); exit; } // Check status: only owner or admin can see pending/rejected $is_admin = isset($_SESSION['role']) && $_SESSION['role'] === 'admin'; $is_owner = $user_id && $car['user_id'] == $user_id; if ($car['status'] !== 'approved' && $car['status'] !== 'sold' && !$is_admin && !$is_owner) { header('Location: ' . APP_URL . 'cars.php'); exit; } $page_title = $car['title'] . " - AFG CARS"; include 'includes/header.php'; // Check if is favorite $is_fav = false; if ($is_logged_in) { // Check if table exists first (soft error prevention) $stmt = $pdo->query("SHOW TABLES LIKE 'favorites'"); if ($stmt->fetch()) { $fs = $pdo->prepare("SELECT id FROM favorites WHERE user_id = ? AND car_id = ?"); $fs->execute([$user_id, $id]); $is_fav = (bool)$fs->fetch(); } } $message = ''; // Handle Purchase Simulation if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['purchase_now'])) { if (!$is_logged_in) { header('Location: ' . APP_URL . 'login.php'); exit; } $bank = $_POST['bank_name'] ?? 'Unknown Bank'; $pdo->beginTransaction(); try { $stmt = $pdo->prepare("INSERT INTO purchases (car_id, buyer_id, payment_method, payment_status, total_amount) VALUES (?, ?, ?, 'completed', ?)"); $stmt->execute([$id, $user_id, $bank, $car['price']]); $stmt = $pdo->prepare("UPDATE cars SET status = 'sold' WHERE id = ?"); $stmt->execute([$id]); $pdo->commit(); $message = "purchase_success"; $car['status'] = 'sold'; // Update local state } catch (Exception $e) { $pdo->rollBack(); $message = "error"; } } // Handle Review if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_review'])) { if (!$is_logged_in) { header('Location: ' . APP_URL . 'login.php'); exit; } $rating = $_POST['rating'] ?? 5; $comment = $_POST['comment'] ?? ''; $stmt = $pdo->prepare("INSERT INTO reviews (user_id, car_id, rating, comment) VALUES (?, ?, ?, ?)"); $stmt->execute([$user_id, $id, $rating, $comment]); header("Location: car-details.php?id=$id&review=success"); exit; } // Fetch Reviews $stmt = $pdo->prepare("SELECT r.*, u.name FROM reviews r JOIN users u ON r.user_id = u.id WHERE r.car_id = ? ORDER BY r.created_at DESC"); $stmt->execute([$id]); $reviews = $stmt->fetchAll(); ?>

SOLD

Views:
$
Year
Fuel
Transmission
Mileage
km
Description

Reviews ()

Leave a Review

Congratulation!

You have successfully purchased this car.

Purchase Options
Listing Inactive

This car is currently unavailable.