prepare("SELECT id FROM users WHERE email = ?"); $stmt->execute([$email]); if ($stmt->fetch()) { $error_message = 'A user with this email already exists.'; } else { // Start transaction $pdo->beginTransaction(); // 1. Create company $stmt = $pdo->prepare("INSERT INTO companies (name) VALUES (?)"); $stmt->execute([$company_name]); $company_id = $pdo->lastInsertId(); // 2. Create admin user $hashed_password = password_hash($password, PASSWORD_DEFAULT); $stmt = $pdo->prepare("INSERT INTO users (company_id, email, password, role) VALUES (?, ?, ?, 'admin')"); $stmt->execute([$company_id, $email, $hashed_password]); $user_id = $pdo->lastInsertId(); // 3. Create a corresponding employee record for the admin user // For simplicity, we'll use the email prefix as first/last name $email_parts = explode('@', $email); $first_name = ucfirst($email_parts[0]); $last_name = 'Admin'; $stmt = $pdo->prepare("INSERT INTO employees (user_id, company_id, first_name, last_name, position) VALUES (?, ?, ?, ?, 'Administrator')"); $stmt->execute([$user_id, $company_id, $first_name, $last_name]); // Commit transaction $pdo->commit(); header('Location: /login.php?registration=success'); exit; } } catch (PDOException $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } $error_message = "Registration failed: " . $e->getMessage(); } } } ?> Register - GPTPayroll
GPTPayroll

Create Your Company Account

Already have an account? Log in here.