<?php
include 'includes/connect.php';

if(isset($_POST['id'])){
    $id = intval($_POST['id']); // sanitize

    // Check if exists
    $check = mysqli_query($conn, "SELECT * FROM category WHERE id='$id'");
    if(mysqli_num_rows($check) > 0){

        $delete = mysqli_query($conn, "DELETE FROM category WHERE id='$id'");
        if($delete){
            echo "<script>
                alert('Category deleted successfully');
                window.location.href='category-list.php';
            </script>";
        } else {
            echo "<script>
                alert('Error: Could not delete category.');
                window.location.href='category-list.php';
            </script>";
        }

    } else {
        echo "<script>
            alert('Category not found.');
            window.location.href='category-list.php';
        </script>";
    }

} else {
    header("Location: category-list.php");
    exit();
}
?>