AFG CARS - Database Setup";
try {
$pdo = db();
echo "Connected to MySQL successfully.
";
// Read schema from database.sql
$sql = file_get_contents(__DIR__ . '/database.sql');
// Execute the full schema
$pdo->exec($sql);
echo "Tables created and basic data inserted from database.sql.
";
// Ensure Admin is correctly hashed
$adminEmail = 'admin@gmail.com';
$adminPass = '12345678';
$hashedPass = password_hash($adminPass, PASSWORD_DEFAULT);
$stmt = $pdo->prepare("UPDATE users SET password = ? WHERE email = ?");
$stmt->execute([$hashedPass, $adminEmail]);
echo "Admin credentials verified (admin@gmail.com / 12345678).
";
// Insert Sample Cars if none exist
$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', 'approved', 'Hot Deal'],
['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', 'approved', 'Hot Deal'],
['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', 'approved', 'Hot Deal'],
['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', 'approved', NULL],
['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', 'approved', NULL]
];
$insert = $pdo->prepare("INSERT INTO cars (title, brand, model, year, price, location, fuel_type, transmission, mileage, description, image, status, badge) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
foreach ($sampleCars as $car) {
$insert->execute($car);
}
echo "Sample car listings added.
";
}
echo "
Database successfully initialized.";
echo "
Go to Homepage";
} catch (PDOException $e) {
echo "
Setup Error: " . $e->getMessage();
}