prepare("SELECT phone, address FROM users WHERE id = ?"); $stmt->execute([$_SESSION['user_id']]); $user_info = $stmt->fetch(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $brand = $_POST['brand'] ?? ''; $model = $_POST['model'] ?? ''; $year = $_POST['year'] ?? ''; $price = $_POST['price'] ?? ''; $city = $_POST['city'] ?? ''; $description = $_POST['description'] ?? ''; $phone = $_POST['phone'] ?? ''; $address = $_POST['address'] ?? ''; // Update user info if provided if ($phone || $address) { $stmt = $pdo->prepare("UPDATE users SET phone = ?, address = ? WHERE id = ?"); $stmt->execute([$phone, $address, $_SESSION['user_id']]); } try { $pdo->beginTransaction(); $stmt = $pdo->prepare("INSERT INTO cars (user_id, brand, model, year, price, city, description, status) VALUES (?, ?, ?, ?, ?, ?, ?, 'pending')"); $stmt->execute([$_SESSION['user_id'], $brand, $model, $year, $price, $city, $description]); $carId = $pdo->lastInsertId(); // Handle Image Upload if (isset($_FILES['car_image']) && $_FILES['car_image']['error'] === UPLOAD_ERR_OK) { $fileTmpPath = $_FILES['car_image']['tmp_name']; $fileName = $_FILES['car_image']['name']; $fileSize = $_FILES['car_image']['size']; $fileType = $_FILES['car_image']['type']; $fileNameCmps = explode(".", $fileName); $fileExtension = strtolower(end($fileNameCmps)); $allowedfileExtensions = array('jpg', 'gif', 'png', 'jpeg', 'webp'); if (in_array($fileExtension, $allowedfileExtensions)) { $uploadFileDir = './assets/images/uploads/'; $newFileName = md5(time() . $fileName) . '.' . $fileExtension; $dest_path = $uploadFileDir . $newFileName; if(move_uploaded_file($fileTmpPath, $dest_path)) { $imagePath = 'assets/images/uploads/' . $newFileName; $stmt = $pdo->prepare("INSERT INTO car_images (car_id, image_path, is_main) VALUES (?, ?, 1)"); $stmt->execute([$carId, $imagePath]); } else { throw new Exception("There was an error moving the uploaded file."); } } else { throw new Exception("Upload failed. Allowed file types: " . implode(',', $allowedfileExtensions)); } } else if (!empty($_POST['image_url'])) { $stmt = $pdo->prepare("INSERT INTO car_images (car_id, image_path, is_main) VALUES (?, ?, 1)"); $stmt->execute([$carId, $_POST['image_url']]); } $pdo->commit(); $success = true; } catch (Exception $e) { $pdo->rollBack(); $error = "Failed to list car: " . $e->getMessage(); } } $cities = ['Kabul', 'Herat', 'Mazar-i-Sharif', 'Kandahar', 'Jalalabad', 'Kunduz', 'Ghazni', 'Balkh']; ?>
Provide details about your car and yourself. Our team will review and approve your listing.
Your car and seller information have been sent for approval. You can track its status in your dashboard.