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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get form data
    $id = $_POST["id"];
    $country = $_POST["country"];
    $state = $_POST["state"];
    $city = $_POST["city"];
    $gender = $_POST["gender"];
    $religion = $_POST["religion"];
    $profile = $_POST["profile"];
    $title = $_POST["title"];

    // Check if a new file is uploaded
    if ($_FILES["file"]["size"] > 0) {
        // Handle image file upload
        $targetDirectory = "uploads/"; // Change this to your upload directory path
        $targetFile = $targetDirectory . basename($_FILES["file"]["name"]);

        if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFile)) {
            // File uploaded successfully
        } else {
            // File upload failed
            echo "Sorry, there was an error uploading your file.";
            exit; // Exit the script
        }
    } else {
        // No new file uploaded, keep the existing file path
        $targetFile = $_POST["existing_file"];
    }

    // Update database
    $sql = "UPDATE add_whatshapp_database SET 
        country = '$country',
        state = '$state',
        city = '$city',
        gender = '$gender',
        religion = '$religion',
        profile = '$profile',
        title = '$title',
        upload_file = '$targetFile'
        WHERE id = $id";

    if (mysqli_query($conn, $sql)) {
        // Update successful
        // echo "Record updated successfully!";

        header("Location: whatsapp-database.php?success=Record Updated successfully!!!");
        exit();
   
    } else {
        // Update failed
        echo "Error updating record: " . mysqli_error($conn);
    }

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