<?php
// Include the database connection file
require_once 'connection.php';

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Escape user inputs for security (assuming connection is established)
    $blocked_by = $conn->real_escape_string($_POST['blocked_by']);
    $user_blocked = $conn->real_escape_string($_POST['user_blocked']);

    // Check if the block record already exists in either direction
    $check_sql = "SELECT * FROM `blockedtable` 
                  WHERE (`blockedBy`='$blocked_by' AND `userBlocked`='$user_blocked')";
                   //  OR (`blockedBy`='$user_blocked' AND `userBlocked`='$blocked_by')";
    $check_result = $conn->query($check_sql);

    if ($check_result->num_rows > 0) {
        // If a block record exists, notify that the user is already blocked
        echo "User is already blocked.";
    } else {
        // Attempt insert query execution if no block record exists
        $insert_sql = "INSERT INTO `blockedtable` (`blockedBy`, `userBlocked`) 
                      VALUES ('$blocked_by', '$user_blocked')";
        
        if ($conn->query($insert_sql) === TRUE) {
            echo "User blocked successfully.";
        } else {
            echo "Error: " . $insert_sql . "<br>" . $conn->error;
        }
    }
}

$conn->close();
?>
