124 lines
5.7 KiB
PHP
124 lines
5.7 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,
|
|
owner_id INT NULL,
|
|
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',
|
|
approval_status ENUM('pending', 'approved', 'rejected') DEFAULT 'approved',
|
|
view_count INT DEFAULT 0,
|
|
badge VARCHAR(50) NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE SET NULL
|
|
) ENGINE=InnoDB;");
|
|
|
|
// Reviews Table
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS reviews (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
user_id INT NOT NULL,
|
|
car_id INT NOT NULL,
|
|
rating INT NOT NULL CHECK (rating >= 1 AND rating <= 5),
|
|
comment TEXT,
|
|
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;");
|
|
|
|
// Notifications Table
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS notifications (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
user_id INT NULL,
|
|
message TEXT NOT NULL,
|
|
type VARCHAR(50) DEFAULT 'info',
|
|
is_read TINYINT(1) DEFAULT 0,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
) ENGINE=InnoDB;");
|
|
|
|
// Purchases Table
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS purchases (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
user_id INT NOT NULL,
|
|
car_id INT NOT NULL,
|
|
amount DECIMAL(12, 2) NOT NULL,
|
|
bank_name VARCHAR(100) NOT NULL,
|
|
transaction_id VARCHAR(100) NOT NULL,
|
|
status ENUM('pending', 'completed', 'failed') DEFAULT 'completed',
|
|
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";
|
|
} else {
|
|
// Update password just in case
|
|
$pass = password_hash('12345678', PASSWORD_DEFAULT);
|
|
$pdo->prepare("UPDATE users SET password = ? WHERE email = ?")->execute([$pass, $adminEmail]);
|
|
echo "Admin password verified/updated.\n";
|
|
}
|
|
|
|
// Insert cars if empty
|
|
$stmt = $pdo->query("SELECT COUNT(*) FROM cars");
|
|
if ($stmt->fetchColumn() == 0) {
|
|
$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']
|
|
];
|
|
|
|
$insert = $pdo->prepare("INSERT INTO cars (title, brand, model, year, price, location, fuel_type, transmission, mileage, description, image_url, approval_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'approved')");
|
|
foreach ($sampleCars as $car) {
|
|
$insert->execute($car);
|
|
}
|
|
echo "Database populated with cars.\n";
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
die("Database Error: " . $e->getMessage());
|
|
} |