File "delete.php"

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

Download   Open   Back

<?php
// Suppress all error output to ensure clean JSON responses
error_reporting(0);
ini_set('display_errors', 0);
ob_start();

require_once '../../config/config.php';
require_once '../helper/audit_log.php';

// Clean any output buffer and set proper headers
ob_clean();
header('Content-Type: application/json');

try {
    if (!isset($_POST['id']) || empty($_POST['id'])) { 
        echo json_encode([
            "status" => "error",
            "message" => "Batch ID is required."
        ]); 
        exit; 
    }
    
    $id = (int)$_POST['id'];
    
    if ($id <= 0) {
        echo json_encode([
            "status" => "error", 
            "message" => "Invalid Batch ID."
        ]);
        exit;
    }
    
    // Check if batch exists
    $check_stmt = $pdo->prepare('SELECT * FROM batches WHERE batch_id = ?');
    $check_stmt->execute([$id]);
    $existing = $check_stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$existing) {
        echo json_encode([
            "status" => "error",
            "message" => "Batch not found."
        ]);
        exit;
    }
    
    // Check if batch is referenced by other tables (students, etc.)
    // Add foreign key checks here if needed
    
    // Delete batch
    $stmt = $pdo->prepare("DELETE FROM batches WHERE batch_id = ?");
    $stmt->execute([$id]);
    
    // Write audit log
    write_audit_log($pdo, [
        'module' => 'Academic Structure',
        'action' => 'delete',
        'entity_table' => 'batches',
        'entity_id' => $id,
        'before_data' => $existing,
        'message' => 'Batch deleted successfully'
    ]);
    
    echo json_encode([
        "status" => "success",
        "message" => "Batch deleted successfully."
    ]);
    
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode([
        "status" => "error",
        "message" => "An error occurred while deleting the batch. Please try again."
    ]);
}
?>










PHP File Manager