79 lines
2.9 KiB
PHP
79 lines
2.9 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Create cars table
|
|
$sql = "CREATE TABLE IF NOT EXISTS cars (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
make VARCHAR(50) NOT NULL,
|
|
model VARCHAR(50) NOT NULL,
|
|
year INT NOT NULL,
|
|
price DECIMAL(10, 2) NOT NULL,
|
|
mileage INT DEFAULT 0,
|
|
image_url VARCHAR(255),
|
|
description TEXT,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)";
|
|
$pdo->exec($sql);
|
|
echo "Table 'cars' created or already exists.<br>";
|
|
|
|
// Check if empty
|
|
$stmt = $pdo->query("SELECT COUNT(*) FROM cars");
|
|
if ($stmt->fetchColumn() == 0) {
|
|
// Seed data
|
|
$cars = [
|
|
[
|
|
'make' => 'Toyota',
|
|
'model' => 'Corolla',
|
|
'year' => 2015,
|
|
'price' => 12500.00,
|
|
'mileage' => 45000,
|
|
'image_url' => 'https://images.pexels.com/photos/170811/pexels-photo-170811.jpeg?auto=compress&cs=tinysrgb&w=800',
|
|
'description' => 'Reliable white Corolla, excellent condition. Kabul plate.'
|
|
],
|
|
[
|
|
'make' => 'Toyota',
|
|
'model' => 'Land Cruiser',
|
|
'year' => 2018,
|
|
'price' => 45000.00,
|
|
'mileage' => 32000,
|
|
'image_url' => 'https://images.pexels.com/photos/116675/pexels-photo-116675.jpeg?auto=compress&cs=tinysrgb&w=800',
|
|
'description' => 'Black Land Cruiser V8. Powerful and luxurious. Ready for any road.'
|
|
],
|
|
[
|
|
'make' => 'Mercedes-Benz',
|
|
'model' => 'C-Class',
|
|
'year' => 2012,
|
|
'price' => 18000.00,
|
|
'mileage' => 60000,
|
|
'image_url' => 'https://images.pexels.com/photos/112460/pexels-photo-112460.jpeg?auto=compress&cs=tinysrgb&w=800',
|
|
'description' => 'Silver C-Class. Imported from Germany. Clean interior.'
|
|
],
|
|
[
|
|
'make' => 'Toyota',
|
|
'model' => 'Hilux',
|
|
'year' => 2020,
|
|
'price' => 28000.00,
|
|
'mileage' => 15000,
|
|
'image_url' => 'https://images.pexels.com/photos/119435/pexels-photo-119435.jpeg?auto=compress&cs=tinysrgb&w=800',
|
|
'description' => 'Double cabin Hilux. Strong engine, barely used.'
|
|
]
|
|
];
|
|
|
|
$insertSql = "INSERT INTO cars (make, model, year, price, mileage, image_url, description) VALUES (:make, :model, :year, :price, :mileage, :image_url, :description)";
|
|
$stmt = $pdo->prepare($insertSql);
|
|
|
|
foreach ($cars as $car) {
|
|
$stmt->execute($car);
|
|
}
|
|
echo "Seeded " . count($cars) . " cars.<br>";
|
|
} else {
|
|
echo "Table 'cars' already has data.<br>";
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
echo "Error: " . $e->getMessage();
|
|
}
|