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

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve data from the form
    $name = $_POST['name'];
    $add = $_POST['add'];
    $number1 = $_POST['number1'];
    $number2 = $_POST['number2'];
    $number3 = $_POST['number3'];
    $company_name = $_POST['company_name'];
    $gmb_page_username = mysqli_real_escape_string($conn, $_POST['gmb_page_username']);

    $facebook_username = mysqli_real_escape_string($conn, $_POST['facebook_username']);
    $twitter_username = mysqli_real_escape_string($conn, $_POST['twitter_username']);
    $instagram_username = mysqli_real_escape_string($conn, $_POST['instagram_username']);
    $youtube_username = mysqli_real_escape_string($conn, $_POST['youtube_username']);
    $currentDateTime = date('Y-m-d H:i:s');

    // Insert data into the database
    $sql = "INSERT INTO add_contact(name, purpose_to_add, number1, number2, number3, company_name, gmb_page, fb, twitter, insta, youtube,datetime) 
            VALUES ('$name', '$add', '$number1', '$number2', '$number3', '$company_name','https://www.google.com/$gmb_page_username',  'https://www.facebook.com/$facebook_username', 'https://twitter.com/$twitter_username', 'https://www.instagram.com/$instagram_username', 'https://www.youtube.com/$youtube_username','$currentDateTime')";
            
    $result = mysqli_query($conn, $sql);

    // Execute the statement
    if ($result) {

        header("Location: add-contact.php?success=Contact Added Successfully!!!");
        exit(); // Exit to prevent further script execution

    } else {
        // Handle insertion failure (display an error message or redirect to an error page)
        echo "Error: " . mysqli_error($conn);
    }
}
?>