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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
 
    $country = $_POST["country"];
    $state = $_POST["state"];
    $city = $_POST["city"];
    $gender = $_POST["gender"];
    $religion = $_POST["religion"];
    $profile = $_POST["profile"];
    $title = $_POST["title"];
    $currentDateTime = date('Y-m-d H:i:s');


    $file_path = "";

    
    if (isset($_FILES["file"])) {
        $file_name = $_FILES["file"]["name"];
        $file_tmp = $_FILES["file"]["tmp_name"];
        
      
        $upload_dir = "uploads/";

    
        if (!file_exists($upload_dir)) {
            mkdir($upload_dir, 0777, true);
        }
        
       
        $file_path = $upload_dir . $file_name;
        if (move_uploaded_file($file_tmp, $file_path)) {

            echo "File uploaded successfully";

        } else {
         
            echo "Error uploading file.";
        }
    }
   
    $sql = "INSERT INTO add_whatshapp_database(country, state, city, gender, religion, profile, title, upload_file,datetime)
            VALUES ('$country', '$state', '$city', '$gender', '$religion', '$profile', '$title', '$file_path','$currentDateTime')";

    if ($conn->query($sql) === TRUE) {
        // echo "Data inserted successfully.";
        header("Location: whatsapp-database.php?success=Data inserted successfully!!!");
        exit();
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
}
?>