setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Create users table $pdo->exec(" CREATE TABLE IF NOT EXISTS `users` ( `id` INT AUTO_INCREMENT PRIMARY KEY, `username` VARCHAR(255) NOT NULL, `email` VARCHAR(255) NOT NULL UNIQUE, `password` VARCHAR(255) NOT NULL, `role` VARCHAR(50) NOT NULL, `birth_date` DATE, `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; "); // Add columns if they don't exist try { $pdo->exec("ALTER TABLE `users` ADD COLUMN `email` VARCHAR(255) NOT NULL UNIQUE AFTER `username`"); } catch (PDOException $e) { /* Ignore */ } try { $pdo->exec("ALTER TABLE `users` ADD COLUMN `birth_date` DATE AFTER `role`"); } catch (PDOException $e) { /* Ignore */ } // We can't easily remove the unique constraint in a single command that works on all versions, so we'll leave it for now. // The signup logic will handle this by checking for existing usernames. // Create clubs table $pdo->exec(" CREATE TABLE IF NOT EXISTS `clubs` ( `id` INT AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(255) NOT NULL, `address` VARCHAR(255) NOT NULL, `number` VARCHAR(50), `neighborhood` VARCHAR(100), `city` VARCHAR(100) NOT NULL, `phone` VARCHAR(100) NULL, `email` VARCHAR(100) NULL, `description` TEXT, `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; "); // Create tournaments table $pdo->exec(" CREATE TABLE IF NOT EXISTS `tournaments` ( `id` INT AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(255) NOT NULL, `description` TEXT, `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; "); // Check if superadmin exists $stmt = $pdo->prepare("SELECT id FROM users WHERE username = 'superadmin'"); $stmt->execute(); if ($stmt->fetch()) { echo "Superadmin user already exists.
"; } else { // Insert default superadmin user $username = 'superadmin'; $email = 'superadmin@picklepro.com'; $password = 'superadmin'; // Default password, you should change this $hashed_password = password_hash($password, PASSWORD_DEFAULT); $role = 'superadmin'; $stmt = $pdo->prepare("INSERT INTO users (username, email, password, role) VALUES (:username, :email, :password, :role)"); $stmt->bindParam(':username', $username); $stmt->bindParam(':email', $email); $stmt->bindParam(':password', $hashed_password); $stmt->bindParam(':role', $role); $stmt->execute(); echo "Superadmin user created successfully with username 'superadmin' and password 'superadmin'.
"; } echo "Database setup completed successfully!
"; echo "You can now delete this file (db/setup.php) for security reasons."; } catch (PDOException $e) { die("Database setup failed: " . $e->getMessage()); }