37650-vm/db/setup_cars.php
Flatlogic Bot 9643b213d0 sad
2026-01-21 08:43:40 +00:00

88 lines
5.4 KiB
PHP

<?php
require_once __DIR__ . '/config.php';
try {
$pdo = db();
echo "Connected to database successfully.<br>";
// Idempotently add columns to the cars table if they don't exist
$columns_to_add = [
'status' => "ALTER TABLE cars ADD COLUMN status VARCHAR(50) NOT NULL DEFAULT 'pending'",
'color' => "ALTER TABLE cars ADD COLUMN color VARCHAR(50)",
'province' => "ALTER TABLE cars ADD COLUMN province VARCHAR(100)",
'city' => "ALTER TABLE cars ADD COLUMN city VARCHAR(100)",
];
$stmt = $pdo->query("DESCRIBE cars");
$existing_columns = $stmt->fetchAll(PDO::FETCH_COLUMN);
foreach ($columns_to_add as $column => $sql) {
if (!in_array($column, $existing_columns)) {
$pdo->exec($sql);
echo "Column '{$column}' added to 'cars' table.<br>";
}
}
// Check if the table is empty before seeding
$stmt = $pdo->query("SELECT COUNT(*) FROM cars");
if ($stmt->fetchColumn() == 0) {
echo "Table 'cars' is empty, proceeding with seeding.<br>";
$cars = [
[
'make' => 'Toyota', 'model' => 'Corolla', 'year' => 2018, 'price' => 13500, 'mileage' => 85000, 'color' => 'White', 'province' => 'Kabul', 'city' => 'Kabul', 'status' => 'approved',
'image_url' => 'https://images.pexels.com/photos/1545743/pexels-photo-1545743.jpeg?auto=compress&cs=tinysrgb&w=800',
'description' => 'A well-maintained Corolla, perfect for city driving. Economical and reliable.'
],
[
'make' => 'Toyota', 'model' => 'Land Cruiser', 'year' => 2020, 'price' => 75000, 'mileage' => 45000, 'color' => 'Black', 'province' => 'Herat', 'city' => 'Herat', 'status' => 'approved',
'image_url' => 'https://images.pexels.com/photos/3764984/pexels-photo-3764984.jpeg?auto=compress&cs=tinysrgb&w=800',
'description' => 'Powerful V8 Land Cruiser. Armored. Ready for any terrain or situation.'
],
[
'make' => 'Mercedes-Benz', 'model' => 'C200', 'year' => 2016, 'price' => 22000, 'mileage' => 72000, 'color' => 'Silver', 'province' => 'Balkh', 'city' => 'Mazar-i-Sharif', 'status' => 'approved',
'image_url' => 'https://images.pexels.com/photos/241316/pexels-photo-241316.jpeg?auto=compress&cs=tinysrgb&w=800',
'description' => 'German luxury and comfort. Smooth ride with a clean interior. -3 plate number.'
],
[
'make' => 'Toyota', 'model' => 'Hilux', 'year' => 2021, 'price' => 35000, 'mileage' => 25000, 'color' => 'Red', 'province' => 'Kandahar', 'city' => 'Kandahar', 'status' => 'approved',
'image_url' => 'https://images.pexels.com/photos/248747/pexels-photo-248747.jpeg?auto=compress&cs=tinysrgb&w=800',
'description' => 'Robust and versatile Hilux pickup. Excellent for both work and family.'
],
[
'make' => 'Honda', 'model' => 'Civic', 'year' => 2019, 'price' => 17000, 'mileage' => 55000, 'color' => 'Blue', 'province' => 'Kabul', 'city' => 'Kabul', 'status' => 'approved',
'image_url' => 'https://images.pexels.com/photos/1637859/pexels-photo-1637859.jpeg?auto=compress&cs=tinysrgb&w=800',
'description' => 'Sporty and modern Honda Civic. Features a sunroof and great fuel economy.'
],
[
'make' => 'Ford', 'model' => 'Ranger', 'year' => 2017, 'price' => 24000, 'mileage' => 95000, 'color' => 'Gray', 'province' => 'Nangarhar', 'city' => 'Jalalabad', 'status' => 'pending',
'image_url' => 'https://images.pexels.com/photos/119435/pexels-photo-119435.jpeg?auto=compress&cs=tinysrgb&w=800',
'description' => 'American toughness. This Ford Ranger is built to handle tough jobs.'
],
[
'make' => 'Toyota', 'model' => 'RAV4', 'year' => 2018, 'price' => 21000, 'mileage' => 62000, 'color' => 'White', 'province' => 'Herat', 'city' => 'Herat', 'status' => 'approved',
'image_url' => 'https://images.pexels.com/photos/707046/pexels-photo-707046.jpeg?auto=compress&cs=tinysrgb&w=800',
'description' => 'Family-friendly SUV. Spacious and comfortable for long journeys.'
],
[
'make' => 'Lexus', 'model' => 'LX 570', 'year' => 2019, 'price' => 85000, 'mileage' => 40000, 'color' => 'Pearl White', 'province' => 'Kabul', 'city' => 'Kabul', 'status' => 'approved',
'image_url' => 'https://images.pexels.com/photos/116675/pexels-photo-116675.jpeg?auto=compress&cs=tinysrgb&w=800',
'description' => 'The pinnacle of luxury and capability. Top-of-the-line model with all options.'
],
];
$insertSql = "INSERT INTO cars (make, model, year, price, mileage, color, province, city, status, image_url, description) VALUES (:make, :model, :year, :price, :mileage, :color, :province, :city, :status, :image_url, :description)";
$stmt = $pdo->prepare($insertSql);
$count = 0;
foreach ($cars as $car) {
$stmt->execute($car);
$count++;
}
echo "Seeded " . $count . " cars into the database.<br>";
} else {
echo "Table 'cars' already contains data. No seeding performed.<br>";
}
} catch (PDOException $e) {
echo "Database error: " . $e->getMessage();
}