37650-vm/sell_car.php
Flatlogic Bot 73e14b3353 sad
2026-01-21 17:27:41 +00:00

159 lines
7.5 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
// Ensure session is started (handled by header usually but we need it for check before header)
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit;
}
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$pdo = db();
// Validate inputs
$make = $_POST['make'] ?? '';
$model = $_POST['model'] ?? '';
$year = $_POST['year'] ?? '';
$price = $_POST['price'] ?? '';
$mileage = $_POST['mileage'] ?? '';
$color = $_POST['color'] ?? '';
$province = $_POST['province'] ?? '';
$city = $_POST['city'] ?? '';
$description = $_POST['description'] ?? '';
$image_url = '';
if (empty($make) || empty($model) || empty($price)) {
$error = "Make, Model, and Price are required.";
} else {
// Handle Image Upload
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$uploadDir = 'assets/images/uploads/';
$fileName = uniqid() . '_' . basename($_FILES['image']['name']);
$targetPath = $uploadDir . $fileName;
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES['image']['tmp_name']);
if($check !== false) {
if (move_uploaded_file($_FILES['image']['tmp_name'], $targetPath)) {
$image_url = $targetPath;
} else {
$error = "Sorry, there was an error uploading your file.";
}
} else {
$error = "File is not an image.";
}
} elseif (!empty($_POST['image_url_input'])) {
$image_url = $_POST['image_url_input'];
}
if (empty($error)) {
try {
$stmt = $pdo->prepare("INSERT INTO cars (user_id, make, model, year, mileage, price, color, province, city, description, image_url, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending')");
$stmt->execute([
$_SESSION['user_id'], $make, $model, $year, $mileage, $price, $color, $province, $city, $description, $image_url
]);
$success = "Your car has been submitted for approval!";
} catch (PDOException $e) {
$error = "Database Error: " . $e->getMessage();
}
}
}
}
$pageTitle = "Sell Your Car";
include 'partials/header.php';
?>
<div class="container section-padding py-5">
<div class="row justify-content-center">
<div class="col-lg-8">
<div class="card shadow border-0">
<div class="card-body p-5">
<h2 class="text-center mb-4">Sell Your Car</h2>
<p class="text-center text-muted mb-5">Fill in the details below to list your car for sale. Our team will review your listing shortly.</p>
<?php if ($error): ?>
<div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
<?php endif; ?>
<form method="POST" enctype="multipart/form-data">
<div class="row g-3">
<div class="col-md-6">
<label class="form-label">Make *</label>
<input type="text" name="make" class="form-control" required placeholder="e.g. Toyota">
</div>
<div class="col-md-6">
<label class="form-label">Model *</label>
<input type="text" name="model" class="form-control" required placeholder="e.g. Camry">
</div>
<div class="col-md-4">
<label class="form-label">Year</label>
<input type="number" name="year" class="form-control" placeholder="2020">
</div>
<div class="col-md-4">
<label class="form-label">Mileage (km)</label>
<input type="number" name="mileage" class="form-control" placeholder="50000">
</div>
<div class="col-md-4">
<label class="form-label">Price ($) *</label>
<input type="number" name="price" class="form-control" required placeholder="15000">
</div>
<div class="col-md-6">
<label class="form-label">Color</label>
<input type="text" name="color" class="form-control" placeholder="White">
</div>
<div class="col-md-6">
<label class="form-label">Province</label>
<select name="province" class="form-select">
<option value="">Select Province</option>
<option value="Kabul">Kabul</option>
<option value="Herat">Herat</option>
<option value="Kandahar">Kandahar</option>
<option value="Mazar-i-Sharif">Mazar-i-Sharif</option>
<option value="Jalalabad">Jalalabad</option>
<option value="Other">Other</option>
</select>
</div>
<div class="col-md-6">
<label class="form-label">City</label>
<input type="text" name="city" class="form-control" placeholder="City Name">
</div>
<div class="col-md-12">
<label class="form-label">Description</label>
<textarea name="description" class="form-control" rows="4" placeholder="Describe the condition, features, etc."></textarea>
</div>
<div class="col-12">
<label class="form-label">Car Image</label>
<div class="input-group mb-2">
<input type="file" name="image" class="form-control" accept="image/*">
</div>
<div class="form-text">Or provide an image URL below if you prefer:</div>
<input type="url" name="image_url_input" class="form-control mt-2" placeholder="https://example.com/car.jpg">
</div>
<div class="col-12 mt-4">
<button type="submit" class="btn btn-primary w-100 py-3 fw-bold">Submit Listing</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php include 'partials/footer.php'; ?>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>