diff --git a/db/config.php b/db/config.php
index e260459..8e2c7d3 100644
--- a/db/config.php
+++ b/db/config.php
@@ -1,17 +1,51 @@
PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
- ]);
- }
- return $pdo;
-}
+ static $pdo;
+ if (!$pdo) {
+ try {
+ $dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4';
+ $pdo = new PDO($dsn, DB_USER, DB_PASS, [
+ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
+ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
+ ]);
+ } catch (PDOException $e) {
+ // Special handling: If database not found, try connecting without DB name
+ // This is useful for the installer script to create the DB
+ if ($e->getCode() == 1049) { // Unknown database
+ $dsn_no_db = 'mysql:host='.DB_HOST.';charset=utf8mb4';
+ try {
+ $pdo = new PDO($dsn_no_db, DB_USER, DB_PASS, [
+ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
+ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
+ ]);
+ } catch (PDOException $ex) {
+ die("Database connection failed: " . $ex->getMessage());
+ }
+ } else {
+ die("Database connection failed: " . $e->getMessage());
+ }
+ }
+ }
+ return $pdo;
+}
\ No newline at end of file
diff --git a/setup_project.php b/install.php
similarity index 75%
rename from setup_project.php
rename to install.php
index b667efc..5f18a85 100644
--- a/setup_project.php
+++ b/install.php
@@ -1,40 +1,65 @@
getMessage() . "\n");
+// Disable output buffering to show progress in real-time
+if (function_exists('apache_setenv')) {
+ @apache_setenv('no-gzip', 1);
}
+@ini_set('zlib.output_compression', 0);
+@ini_set('implicit_flush', 1);
+for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
+ob_implicit_flush(1);
+
+echo "
Installation";
+echo "Car Sells in Afghanistan - Installation
";
try {
- // 2. Drop existing tables to ensure a clean state
- $pdo->exec("SET FOREIGN_KEY_CHECKS=0");
- $pdo->exec("DROP TABLE IF EXISTS reviews");
- $pdo->exec("DROP TABLE IF EXISTS bookings");
- $pdo->exec("DROP TABLE IF EXISTS cars");
- $pdo->exec("DROP TABLE IF EXISTS users");
- $pdo->exec("SET FOREIGN_KEY_CHECKS=1");
- echo "Existing tables dropped.\n";
+ // 1. Connect to Database Server
+ $pdo = db();
+ echo "✅ Connected to Database Server.
";
- // 3. Create tables
+ // 2. Create Database (if it doesn't exist)
+ // Note: On some hosting/VMs, the user might not have permission to create databases, only tables.
+ // We try to create it if we are 'root' or similar, but otherwise assume it exists if connection worked.
+ $dbName = DB_NAME;
+ try {
+ $pdo->exec("CREATE DATABASE IF NOT EXISTS `$dbName` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
+ echo "✅ Database `$dbName` checked/created.
";
+ } catch (PDOException $e) {
+ // Ignore if we can't create DB (might already be connected to it)
+ echo "ℹ️ Note: Could not create database (might already exist or permission denied). Proceeding...
";
+ }
+
+ // Select the database
+ $pdo->exec("USE `$dbName`");
+
+ // 3. Drop existing tables (Clean Install)
+ $pdo->exec("SET FOREIGN_KEY_CHECKS=0");
+ $tables = ['reviews', 'bookings', 'cars', 'users'];
+ foreach ($tables as $table) {
+ $pdo->exec("DROP TABLE IF EXISTS `$table`");
+ }
+ $pdo->exec("SET FOREIGN_KEY_CHECKS=1");
+ echo "✅ Existing tables dropped (Clean Install).
";
+
+ // 4. Create Tables
// Users Table
- $pdo->exec(" CREATE TABLE users (
+ $pdo->exec("CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
+ email VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
role VARCHAR(20) DEFAULT 'user',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");
- echo "Users table ready.\n";
+ echo "✅ Table `users` created.
";
// Cars Table
- $pdo->exec(" CREATE TABLE cars (
+ $pdo->exec("CREATE TABLE cars (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NULL,
title VARCHAR(255) NULL,
@@ -52,10 +77,10 @@ try {
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
)");
- echo "Cars table ready.\n";
+ echo "✅ Table `cars` created.
";
// Bookings Table
- $pdo->exec(" CREATE TABLE bookings (
+ $pdo->exec("CREATE TABLE bookings (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
car_id INT NOT NULL,
@@ -67,10 +92,10 @@ try {
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE CASCADE
)");
- echo "Bookings table ready.\n";
+ echo "✅ Table `bookings` created.
";
// Reviews Table
- $pdo->exec(" CREATE TABLE reviews (
+ $pdo->exec("CREATE TABLE reviews (
id INT AUTO_INCREMENT PRIMARY KEY,
car_id INT NOT NULL,
user_id INT NOT NULL,
@@ -81,30 +106,20 @@ try {
FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
)");
- echo "Reviews table ready.\n";
+ echo "✅ Table `reviews` created.
";
- // 4. Add default admin user
- $adminUsername = 'admin@gmail.com';
- $adminPassword = '12345678';
- $adminHash = password_hash($adminPassword, PASSWORD_DEFAULT);
-
- // Check if admin exists (though we dropped tables, good practice)
- $stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE username = ?");
- $stmt->execute([$adminUsername]);
-
- if ($stmt->fetchColumn() == 0) {
- $insertAdmin = $pdo->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, 'admin')");
- $insertAdmin->execute([$adminUsername, $adminHash]);
- $adminId = $pdo->lastInsertId();
- echo "Admin user created (User: $adminUsername, Pass: $adminPassword).\n";
- } else {
- echo "Admin user already exists.\n";
- $stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
- $stmt->execute([$adminUsername]);
- $adminId = $stmt->fetchColumn();
- }
+ // 5. Create Default Admin User
+ $adminUser = 'admin';
+ $adminEmail = 'admin@gmail.com';
+ $adminPass = '123'; // As requested
+ $adminHash = password_hash($adminPass, PASSWORD_DEFAULT);
- // 5. Insert sample data (15 Cars)
+ $stmt = $pdo->prepare("INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, 'admin')");
+ $stmt->execute([$adminUser, $adminEmail, $adminHash]);
+ $adminId = $pdo->lastInsertId();
+ echo "✅ Admin user created.
Username: $adminUser
Email: $adminEmail
Password: $adminPass
";
+
+ // 6. Insert Sample Data (Cars)
$carsData = [
[
'title' => 'Toyota Corolla 2020 Clean',
@@ -286,7 +301,7 @@ try {
'color' => 'Red',
'province' => 'Mazar-i-Sharif',
'city' => 'Mazar',
- 'image_url' => 'https://images.pexels.com/photos/35967/mini-cooper-auto-model-vehicle.jpg?auto=compress&cs=tinysrgb&w=600' // Placeholder for small car
+ 'image_url' => 'https://images.pexels.com/photos/35967/mini-cooper-auto-model-vehicle.jpg?auto=compress&cs=tinysrgb&w=600'
],
[
'title' => 'Mazda 6 2019',
@@ -324,10 +339,14 @@ try {
$car['user_id'] = $adminId;
$insertCar->execute($car);
}
- echo "Seed data inserted (" . count($carsData) . " cars).\n";
+ echo "✅ Seed data inserted (" . count($carsData) . " cars).
";
- echo "Setup complete. The application is ready to use.\n";
+ echo "
🎉 Installation Complete!
";
+ echo "You can now Login here.
";
+ echo "Credentials:
Username: admin or admin@gmail.com
Password: 123
";
} catch (PDOException $e) {
- die("Setup failed: " . $e->getMessage() . "\n");
-}
\ No newline at end of file
+ echo "❌ Installation Failed: " . htmlspecialchars($e->getMessage()) . "
";
+}
+echo "";
+?>
diff --git a/login.php b/login.php
index 202510d..6b8913d 100644
--- a/login.php
+++ b/login.php
@@ -14,12 +14,14 @@ if (isset($_SESSION['user_id'])) {
}
$errors = [];
+$login_input = ''; // Store input to repopulate form
+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $username = trim($_POST['username'] ?? '');
+ $login_input = trim($_POST['username'] ?? ''); // This field now accepts user OR email
$password = $_POST['password'] ?? '';
- if (empty($username)) {
- $errors[] = 'Username is required.';
+ if (empty($login_input)) {
+ $errors[] = 'Username or Email is required.';
}
if (empty($password)) {
$errors[] = 'Password is required.';
@@ -28,20 +30,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (empty($errors)) {
try {
$pdo = db();
- // Allow login by username only per new schema
- $stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username LIMIT 1");
- $stmt->execute(['username' => $username]);
+ // Allow login by username OR email
+ $stmt = $pdo->prepare("SELECT * FROM users WHERE username = :input OR email = :input LIMIT 1");
+ $stmt->execute(['input' => $login_input]);
$user = $stmt->fetch();
// Note: The 'password' column stores the hash
if ($user && password_verify($password, $user['password'])) {
- if (isset($user['status']) && $user['status'] !== 'active') {
- // Kept specific status check logic if status column existed, but since schema is simple, this block is mostly for safety if schema evolves.
- // Current schema doesn't have status, but if it did, we'd check it.
- // The setup_project.php removed the status column from users table to fit the simple requirements.
- // So we proceed.
- }
-
+ // Login Success
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'];
@@ -94,7 +90,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {