<?php
require 'db_config.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Retrieve and sanitize form data
    $id = $_POST['id'] ?? '';
    $fullname = $_POST['fullname'] ?? '';
    $email = $_POST['email'] ?? '';
    $phone = $_POST['phone'] ?? '';
    $payment_status = $_POST['payment_status'] ?? '';
    $hotel = $_POST['hotel'] ?? '';
    $institution = $_POST['institution'] ?? '';
    $membership_type = $_POST['membership_type'] ?? '';

    // Convert promotion_material array to a string
    $promotion_material = isset($_POST['promotion_material']) ? implode(',', $_POST['promotion_material']) : null;

    // Update user data in the database
    $stmt = $conn->prepare("UPDATE users SET fullname=?, email=?, phone=?, payment_status=?, hotel=?, institution=?, membership_type=?, promotion_material=? WHERE id=?");
    $stmt->bind_param("ssssssssi", $fullname, $email, $phone, $payment_status, $hotel, $institution, $membership_type, $promotion_material, $id);

    if ($stmt->execute()) {
        echo "User updated successfully.";
    } else {
        echo "Error updating user: " . $conn->error;
    }

    $stmt->close();
}

$conn->close();
?>
