prepare('SELECT id FROM users WHERE email = ?'); $stmt->execute([$email]); if ($stmt->fetch()) { $errors[] = 'User with this email already exists'; } else { $pdo->beginTransaction(); // Check if community for the city exists $stmt = $pdo->prepare('SELECT id FROM communities WHERE name = ?'); $stmt->execute([$city]); $community = $stmt->fetch(); $role = 'member'; if (!$community) { $role = 'leader'; } // Insert the new user $hashed_password = password_hash($password, PASSWORD_DEFAULT); $stmt = $pdo->prepare('INSERT INTO users (name, email, password, city, role) VALUES (?, ?, ?, ?, ?)'); $stmt->execute([$name, $email, $hashed_password, $city, $role]); $user_id = $pdo->lastInsertId(); if ($role === 'leader') { $stmt = $pdo->prepare('INSERT INTO communities (name, leader_id) VALUES (?, ?)'); $stmt->execute([$city, $user_id]); $community_id = $pdo->lastInsertId(); } else { $community_id = $community['id']; } // Add user to community $stmt = $pdo->prepare('INSERT INTO community_members (user_id, community_id) VALUES (?, ?)'); $stmt->execute([$user_id, $community_id]); $pdo->commit(); $_SESSION['user_id'] = $user_id; header('Location: communities.php'); exit; } } catch (PDOException $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } $errors[] = 'Database error: ' . $e->getMessage(); } } } ?> Sign Up - Community Hub

Create Your Account