exec("CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); $pdo = new PDO("mysql:host=$host;dbname=$name", $user, $pass); $config_content = " PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]); } return \$pdo; } require_once __DIR__ . '/../includes/functions.php'; "; file_put_contents($config_file, $config_content); header("Location: install.php?step=2"); exit; } catch (Exception $e) { $error = "Database Connection Failed: " . $e->getMessage(); } } elseif ($step === 2) { require_once $config_file; try { $pdo = db(); // 1. Run Base Schema $schema = file_get_contents(__DIR__ . '/db/schema.sql'); // Split schema by ; to execute one by one to avoid issues with some PDO drivers // But usually PDO::exec handles multiple if allowed. // Better to use a simpler approach for schema.sql $pdo->exec($schema); // 2. Create migrations table if not exists $pdo->exec("CREATE TABLE IF NOT EXISTS migrations ( id INT AUTO_INCREMENT PRIMARY KEY, migration VARCHAR(255) UNIQUE NOT NULL, applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"); // 3. Run Migrations $migrations_dir = __DIR__ . '/db/migrations/'; $migrations = glob($migrations_dir . '*.sql'); sort($migrations); // Get applied migrations $stmt = $pdo->query("SELECT migration FROM migrations"); $applied = $stmt->fetchAll(PDO::FETCH_COLUMN); foreach ($migrations as $migration_path) { $migration_name = basename($migration_path); if (!in_array($migration_name, $applied)) { $sql = file_get_contents($migration_path); if (!empty(trim($sql))) { // Some migrations might fail because they were partially applied before we had tracking // We will wrap each in a try-catch or just execute and catch specific duplicate column errors try { $pdo->exec($sql); $stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)"); $stmt->execute([$migration_name]); } catch (PDOException $e) { // If it's "Duplicate column name" or "Duplicate key name", we might want to skip and mark as applied // to recover from a broken state if (strpos($e->getMessage(), 'Duplicate column name') !== false || strpos($e->getMessage(), 'Duplicate key name') !== false || strpos($e->getMessage(), 'already exists') !== false) { $stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)"); $stmt->execute([$migration_name]); } else { throw $e; } } } } } // 4. Seed initial data (from init.php logic) $stmt = $pdo->query("SELECT COUNT(*) FROM outlets"); if ($stmt->fetchColumn() == 0) { $pdo->exec("INSERT INTO outlets (name, address) VALUES ('Main Downtown', '123 Main St'), ('Westside Hub', '456 West Blvd')"); $pdo->exec("INSERT INTO categories (name, sort_order) VALUES ('Burgers', 1), ('Sides', 2), ('Drinks', 3)"); $pdo->exec("INSERT INTO products (category_id, name, description, price) VALUES (1, 'Signature Burger', 'Juicy beef patty with special sauce', 12.99), (1, 'Veggie Delight', 'Plant-based patty with fresh avocado', 11.50), (2, 'Truffle Fries', 'Crispy fries with truffle oil and parmesan', 5.99), (3, 'Craft Cola', 'House-made sparkling cola', 3.50)"); } header("Location: install.php?step=3"); exit; } catch (Exception $e) { $error = "Initialization Failed: " . $e->getMessage(); } } elseif ($step === 3) { require_once $config_file; $username = $_POST['admin_user'] ?? ''; $password = $_POST['admin_pass'] ?? ''; $email = $_POST['admin_email'] ?? ''; if (empty($username) || empty($password)) { $error = "Username and Password are required."; } else { try { $pdo = db(); $hashed_pass = password_hash($password, PASSWORD_DEFAULT); // Ensure Admin group exists $stmt = $pdo->prepare("SELECT id FROM user_groups WHERE name = 'Administrator' LIMIT 1"); $stmt->execute(); $group_id = $stmt->fetchColumn(); if (!$group_id) { $pdo->exec("INSERT INTO user_groups (name, permissions) VALUES ('Administrator', 'all')"); $group_id = $pdo->lastInsertId(); } $stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?"); $stmt->execute([$username]); if ($stmt->fetch()) { // Admin already exists, just move to next step header("Location: install.php?step=4"); exit; } $stmt = $pdo->prepare("INSERT INTO users (group_id, username, password, full_name, email, is_active) VALUES (?, ?, ?, ?, ?, 1)"); $stmt->execute([$group_id, $username, $hashed_pass, 'Super Admin', $email]); header("Location: install.php?step=4"); exit; } catch (Exception $e) { $error = "Failed to create Admin: " . $e->getMessage(); } } } } ?>
Enter your database connection details.
Ready to create tables and run migrations. This will set up the latest database structure.
Create your first administrator account.
The system has been successfully installed.
install.php file from your server immediately!