<?php
include('connection.php');

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $newMemberName = mysqli_real_escape_string($conn, $_POST['newMemberName']);
    $groupId = mysqli_real_escape_string($conn, $_POST['groupId']);


    // Check if the group exists before updating
    $checkGroupQuery = "SELECT * FROM groups WHERE id = '$groupId'";
    $result = mysqli_query($conn, $checkGroupQuery);
    if (mysqli_num_rows($result) > 0) {
        // Add the new member to the specified group in your database
        $sql = "UPDATE groups SET member = CONCAT(member, '$newMemberName,') WHERE id = '$groupId'";
        
        if (mysqli_query($conn, $sql)) {

            // echo 'Member added successfully.';
            header("Location: view-all-group.php?success = Member added successfully!!!");
            exit(); // Exit to prevent further script execution

        } else {
            echo 'Error updating member: ' . mysqli_error($conn);
        }
    } else {
        echo 'Group not found.';
    }
}

mysqli_close($conn);
?>