prepare("INSERT INTO teams (name, owner_user_id) VALUES (?, ?)"); $stmt->execute([$team_name, $user_id]); $team_id = $pdo->lastInsertId(); // Automatically add the creator as a member with the 'owner' role $stmt = $pdo->prepare("INSERT INTO team_members (team_id, user_id, role) VALUES (?, ?, 'owner')"); $stmt->execute([$team_id, $user_id]); header('Location: teams.php'); // Redirect to avoid form resubmission exit(); } catch (PDOException $e) { error_log("Database error in teams.php (create team): " . $e->getMessage()); $error_message = "Failed to create team. Please try again."; } } } // Fetch teams for the current user try { $stmt = $pdo->prepare( "SELECT t.id, t.name, t.owner_user_id FROM teams t " . "JOIN team_members tm ON t.id = tm.team_id " . "WHERE tm.user_id = ?" ); $stmt->execute([$user_id]); $teams = $stmt->fetchAll(); } catch (PDOException $e) { error_log("Database error in teams.php (fetch teams): " . $e->getMessage()); $error_message = "Failed to load teams. Please contact support."; } require_once 'partials/header.php'; ?>