query("SELECT id, name, price, features FROM packages ORDER BY price"); $packages = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $errors[] = "Database error: " . $e->getMessage(); $packages = []; } if ($_SERVER["REQUEST_METHOD"] == "POST") { $username = trim($_POST['username'] ?? ''); $email = trim($_POST['email'] ?? ''); $password = $_POST['password'] ?? ''; $password_confirm = $_POST['password_confirm'] ?? ''; $package_id = $_POST['package_id'] ?? ''; // Validation if (empty($username)) $errors[] = 'Username is required.'; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = 'A valid email is required.'; if (strlen($password) < 8) $errors[] = 'Password must be at least 8 characters long.'; if ($password !== $password_confirm) $errors[] = 'Passwords do not match.'; if (empty($package_id)) $errors[] = 'Please select a package.'; // Check if user already exists if (empty($errors)) { try { $pdo = db(); $stmt = $pdo->prepare("SELECT id FROM users WHERE username = :username OR email = :email"); $stmt->execute(['username' => $username, 'email' => $email]); if ($stmt->fetch()) { $errors[] = 'Username or email already exists.'; } } catch (PDOException $e) { $errors[] = "Database error: " . $e->getMessage(); } } // Insert new user if (empty($errors)) { $hashed_password = password_hash($password, PASSWORD_DEFAULT); try { $pdo = db(); $stmt = $pdo->prepare("INSERT INTO users (username, email, password, role, package_id) VALUES (:username, :email, :password, 'artist', :package_id)"); $stmt->execute(['username' => $username, 'email' => $email, 'password' => $hashed_password, 'package_id' => $package_id]); $successMessage = "Registration successful! You can now log in."; } catch (PDOException $e) { $errors[] = "Database error during registration: " . $e->getMessage(); } } } ?>