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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get form data
    $cg_name = $_POST['cg_name'];
    $cg_url = $_POST['cg_url'];
    $cg_type = $_POST['cg_type'];
    $cg_subscriber = $_POST['cg_subscriber'];
    $currentDateTime = date('Y-m-d H:i:s'); // Get current date and time

    // Prepare the select query to check for any similar record
    $select_query = "SELECT * FROM telegram WHERE (cg_name = '$cg_name' AND cg_url = '$cg_url'  AND cg_type = '$cg_type' AND cg_subscriber = '$cg_subscriber')";
    $select_result = mysqli_query($conn, $select_query);

    if (mysqli_num_rows($select_result) == 0) {
        // No similar record found, proceed to insert the new record
        $insert_query = "INSERT INTO telegram (cg_name, cg_url, cg_type, cg_subscriber, date_time) VALUES ('$cg_name', '$cg_url', '$cg_type', '$cg_subscriber', '$currentDateTime')";
        $insert_result = mysqli_query($conn, $insert_query);

        if ($insert_result) {
            // Record added successfully
            header("Location: add_telegram.php?success=Data%20Added%20Successfully!");
            exit();
        } else {
            // Error occurred while adding record
            header("Location: add_telegram.php?error=Failed%20to%20Insert%20Record!!!");
            exit();
        }
    } else {
        // A similar record already exists, display an error message
        header("Location: add_telegram.php?error=Data%20Already%20Exists");
        exit();
    }
}
?>
