<?php
session_name("admin_session");
session_start();
// Include database connection file
include("connection.php");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get form data
    $name = $_POST['name'];
    $url = $_POST['url'];
    $number = $_POST['number'];
    $added_by=$_SESSION['username'];
    $currentDateTime = date('Y-m-d H:i:s'); // Get current date and time

    // Check if the product already exists
    $checkDuplicate = "SELECT * FROM our_skype WHERE name = '$name' AND url = '$url' AND number = '$number'";
    $resultDuplicate = mysqli_query($conn, $checkDuplicate);
    if (mysqli_num_rows($resultDuplicate) > 0) {
        // Product already exists, show a SweetAlert error message
        header("Location: our_skype.php?msg=3");
        exit();
    } else {

        // Prepare the insert statement
        $insert_query = "INSERT INTO our_skype (name, url, number, added_by, datetime) VALUES ('$name', '$url', '$number', '$added_by','$currentDateTime')";

        // Execute the query
        $insert_result = mysqli_query($conn, $insert_query);

        if ($insert_result) {
            // Redirect back to the form page with success message
            header("Location: our_skype.php?msg=1");
            exit();
        } else {
            // Redirect back to the form page with error message
            header("Location: our_skype.php?msg=2");
            exit();
        }
    }
}