File "delete.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/categories/delete.php
File size: 1.3 KiB (1336 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

<?php
require_once '../../config/config.php';
require_once '../helper/audit_log.php';
header('Content-Type: application/json');

try {
    $input = json_decode(file_get_contents('php://input'), true);
    $id    = (int)($input['id'] ?? 0);

    if ($id <= 0) {
        echo json_encode([
            "status"  => "error",
            "message" => "Invalid ID."
        ]);
        exit;
    }

    // Fetch before delete
    $before = null;
    try {
        $b = $pdo->prepare('SELECT * FROM categories WHERE category_id = ?');
        $b->execute([$id]);
        $before = $b->fetch(PDO::FETCH_ASSOC);
    } catch (Throwable $e) {}

    $stmt = $pdo->prepare("DELETE FROM categories WHERE category_id = ?");
    $stmt->execute([$id]);

    write_audit_log($pdo, [
        'module'       => 'Academic Structure',
        'action'       => 'delete',
        'entity_table' => 'categories',
        'entity_id'    => $id,
        'before_data'  => $before,
        'message'      => 'Category deleted'
    ]);

    echo json_encode([
        "status"  => "success",
        "message" => "Category deleted successfully."
    ]);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode([
        "status"  => "error",
        "message" => $e->getMessage()
    ]);
}
?>

PHP File Manager