<?php
session_start();
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: index.php');
    exit;
}
include 'dbconn.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id'])) {
    $id                 = intval($_POST['id']);
    $main_gam           = trim($_POST['main_gam']);
    $name               = trim($_POST['name']);
    $dob                = $_POST['dob'];
    $marital_status     = $_POST['marital_status'];
    $mosal              = trim($_POST['mosal']);
    $education          = trim($_POST['education']);
    $blood_group        = $_POST['family_blood_group'][0] ?? '';
    $business_address   = trim($_POST['business_address']);
    $residential_address= trim($_POST['residential_address']);
    $area               = trim($_POST['area']);
    $mobile             = trim($_POST['mobile']);
    $email              = trim($_POST['email']);

    $query = "UPDATE members SET 
        main_gam = ?, 
        name = ?, 
        dob = ?, 
        marital_status = ?, 
        mosal = ?, 
        education = ?, 
        blood = ?, 
        b_add = ?, 
        r_add = ?, 
        area = ?, 
        mobile = ?, 
        email = ?
        WHERE id = ?";

    $stmt = $mysqli->prepare($query);
    $stmt->bind_param("ssssssssssssi", 
        $main_gam, $name, $dob, $marital_status, $mosal, $education, 
        $blood_group, $business_address, $residential_address, 
        $area, $mobile, $email, $id);

    if ($stmt->execute()) {
        header("Location: manage_members.php?updated=1");
        exit;
    } else {
        echo "Error updating member: " . $stmt->error;
    }
} else {
    echo "Invalid request.";
}
?>