<?php
include('connection.php'); // Include your database connection file

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = $_POST['title']; // Get the group name from the form
    $members = $_POST['member']; // Get an array of selected members from the form

    // Initialize an empty string to store the members
    $membersString = '';

    // Loop through the selected members and append them to the string
    foreach ($members as $member) {
        $membersString .= $member . ',';
    }

    // Remove the trailing comma
    $membersString = rtrim($membersString, ',');

    // Insert the group name and members into a single table
    $sql = "INSERT INTO groups (group_name, member) VALUES ('$title', '$membersString')";
    
    if (mysqli_query($conn, $sql)) {

        // echo "Group and members added successfully.";
        header("Location: create_group.php?success=Group and members added successfully!!!");
        exit(); // Exit to prevent further script execution

    } else {
        echo "Error: " . mysqli_error($conn);
    }
}

mysqli_close($conn); // Close the database connection
?>
