112 lines
8.0 KiB
PHP
112 lines
8.0 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Users Table
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS users (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
full_name VARCHAR(100) NOT NULL,
|
|
email VARCHAR(100) NOT NULL UNIQUE,
|
|
password VARCHAR(255) NOT NULL,
|
|
phone VARCHAR(20),
|
|
role ENUM('admin', 'user') DEFAULT 'user',
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
) ENGINE=InnoDB;");
|
|
|
|
// Cars Table
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS cars (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
title VARCHAR(255) NOT NULL,
|
|
brand VARCHAR(100) NOT NULL,
|
|
model VARCHAR(100) NOT NULL,
|
|
year INT NOT NULL,
|
|
price DECIMAL(12, 2) NOT NULL,
|
|
location VARCHAR(100) NOT NULL,
|
|
fuel_type VARCHAR(50),
|
|
transmission VARCHAR(50),
|
|
mileage INT,
|
|
description TEXT,
|
|
image_url VARCHAR(255),
|
|
status ENUM('available', 'sold') DEFAULT 'available',
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
) ENGINE=InnoDB;");
|
|
|
|
// Bookings Table
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS bookings (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
user_id INT NOT NULL,
|
|
car_id INT NOT NULL,
|
|
message TEXT,
|
|
status ENUM('pending', 'confirmed', 'cancelled') DEFAULT 'pending',
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
|
FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE CASCADE
|
|
) ENGINE=InnoDB;");
|
|
|
|
// Favorites Table
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS favorites (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
user_id INT NOT NULL,
|
|
car_id INT NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
UNIQUE KEY user_car (user_id, car_id),
|
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
|
FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE CASCADE
|
|
) ENGINE=InnoDB;");
|
|
|
|
// Create Admin if not exists
|
|
$adminEmail = 'admin@gmail.com';
|
|
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
|
|
$stmt->execute([$adminEmail]);
|
|
if (!$stmt->fetch()) {
|
|
$pass = password_hash('12345678', PASSWORD_DEFAULT);
|
|
$pdo->prepare("INSERT INTO users (full_name, email, password, role) VALUES (?, ?, ?, ?)")
|
|
->execute(['Admin User', $adminEmail, $pass, 'admin']);
|
|
echo "Admin user created (admin@gmail.com / 12345678)\n";
|
|
}
|
|
|
|
// Insert 20 cars if empty
|
|
$stmt = $pdo->query("SELECT COUNT(*) FROM cars");
|
|
if ($stmt->fetchColumn() < 20) {
|
|
$pdo->exec("SET FOREIGN_KEY_CHECKS = 0");
|
|
$pdo->exec("TRUNCATE TABLE cars");
|
|
$pdo->exec("TRUNCATE TABLE bookings");
|
|
$pdo->exec("TRUNCATE TABLE favorites");
|
|
$pdo->exec("SET FOREIGN_KEY_CHECKS = 1");
|
|
|
|
$sampleCars = [
|
|
['Toyota Corolla 2022', 'Toyota', 'Corolla', 2022, 18500, 'Kabul', 'Petrol', 'Automatic', 12000, 'Like new condition, full options.', 'https://images.pexels.com/photos/3311574/pexels-photo-3311574.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Lexus LX570 2018', 'Lexus', 'LX570', 2018, 75000, 'Herat', 'Petrol', 'Automatic', 45000, 'Powerful SUV, VIP interior.', 'https://images.pexels.com/photos/170811/pexels-photo-170811.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Mercedes-Benz C300', 'Mercedes-Benz', 'C300', 2020, 32000, 'Mazar-i-Sharif', 'Petrol', 'Automatic', 25000, 'Excellent fuel efficiency and comfort.', 'https://images.pexels.com/photos/120049/pexels-photo-120049.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Toyota Land Cruiser Prado', 'Toyota', 'Prado', 2019, 55000, 'Kabul', 'Diesel', 'Automatic', 30000, 'Perfect for off-road and city driving.', 'https://images.pexels.com/photos/112460/pexels-photo-112460.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Hyundai Elantra', 'Hyundai', 'Elantra', 2021, 21000, 'Kabul', 'Petrol', 'Automatic', 15000, 'Modern design and great fuel economy.', 'https://images.pexels.com/photos/3752162/pexels-photo-3752162.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Kia Sportage', 'Kia', 'Sportage', 2022, 28000, 'Herat', 'Petrol', 'Automatic', 8000, 'Latest model, panoramic sunroof.', 'https://images.pexels.com/photos/358070/pexels-photo-358070.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Toyota Hilux', 'Toyota', 'Hilux', 2020, 35000, 'Kandahar', 'Diesel', 'Manual', 40000, 'Reliable workhorse, 4x4.', 'https://images.pexels.com/photos/1035108/pexels-photo-1035108.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Honda Civic', 'Honda', 'Civic', 2017, 15000, 'Mazar-i-Sharif', 'Petrol', 'Automatic', 60000, 'Sporty look, well maintained.', 'https://images.pexels.com/photos/1149137/pexels-photo-1149137.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Lexus RX350', 'Lexus', 'RX350', 2016, 28000, 'Kabul', 'Petrol', 'Automatic', 75000, 'Luxury SUV, smooth ride.', 'https://images.pexels.com/photos/116675/pexels-photo-116675.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Nissan Patrol', 'Nissan', 'Patrol', 2015, 42000, 'Jalalabad', 'Petrol', 'Automatic', 90000, 'Desert king, powerful V8 engine.', 'https://images.pexels.com/photos/1637859/pexels-photo-1637859.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Toyota Camry 2023', 'Toyota', 'Camry', 2023, 31000, 'Kabul', 'Hybrid', 'Automatic', 5000, 'Eco-friendly and premium comfort.', 'https://images.pexels.com/photos/210019/pexels-photo-210019.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['BMW X5', 'BMW', 'X5', 2018, 45000, 'Kabul', 'Petrol', 'Automatic', 55000, 'High performance and luxury features.', 'https://images.pexels.com/photos/1007410/pexels-photo-1007410.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Ford Mustang', 'Ford', 'Mustang', 2019, 38000, 'Herat', 'Petrol', 'Automatic', 20000, 'Iconic muscle car, head-turner.', 'https://images.pexels.com/photos/337909/pexels-photo-337909.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Volkswagen Golf', 'Volkswagen', 'Golf', 2017, 14000, 'Mazar-i-Sharif', 'Diesel', 'Manual', 85000, 'Compact and efficient daily driver.', 'https://images.pexels.com/photos/1149831/pexels-photo-1149831.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Toyota RAV4', 'Toyota', 'RAV4', 2021, 29000, 'Kabul', 'Hybrid', 'Automatic', 12000, 'Smart SUV for modern lifestyle.', 'https://images.pexels.com/photos/3156482/pexels-photo-3156482.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Mercedes-Benz G-Class', 'Mercedes-Benz', 'G-Wagon', 2020, 180000, 'Kabul', 'Petrol', 'Automatic', 15000, 'Luxury status symbol, armored glass.', 'https://images.pexels.com/photos/100656/pexels-photo-100656.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Hyundai Tucson', 'Hyundai', 'Tucson', 2022, 27500, 'Herat', 'Petrol', 'Automatic', 10000, 'Modern tech and spacious interior.', 'https://images.pexels.com/photos/3972755/pexels-photo-3972755.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Kia Sorento', 'Kia', 'Sorento', 2021, 32000, 'Kabul', 'Diesel', 'Automatic', 18000, '7-seater family SUV, great for long trips.', 'https://images.pexels.com/photos/1149137/pexels-photo-1149137.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Honda Accord', 'Honda', 'Accord', 2018, 19500, 'Mazar-i-Sharif', 'Petrol', 'Automatic', 45000, 'Reliable sedan with premium feel.', 'https://images.pexels.com/photos/3802510/pexels-photo-3802510.jpeg?auto=compress&cs=tinysrgb&w=800'],
|
|
['Toyota 4Runner', 'Toyota', '4Runner', 2017, 34000, 'Kabul', 'Petrol', 'Automatic', 70000, 'Legendary reliability, off-road ready.', 'https://images.pexels.com/photos/1592384/pexels-photo-1592384.jpeg?auto=compress&cs=tinysrgb&w=800']
|
|
];
|
|
|
|
$insert = $pdo->prepare("INSERT INTO cars (title, brand, model, year, price, location, fuel_type, transmission, mileage, description, image_url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
|
foreach ($sampleCars as $car) {
|
|
$insert->execute($car);
|
|
}
|
|
echo "Database populated with 20 cars.\n";
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
die("Database Error: " . $e->getMessage());
|
|
}
|