File "delete.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/exam_timetable/delete.php
File size: 1.32 KiB (1348 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 {
    $id = (int)($_POST['id'] ?? 0);
    if ($id<=0) { echo json_encode(['status'=>'error','message'=>'ID required']); exit; }

    // Protect if allocations depend on timetable
    $dep = $pdo->prepare('SELECT COUNT(*) FROM hall_allocations WHERE timetable_id = ?');
    $dep->execute([$id]);
    if ((int)$dep->fetchColumn() > 0) { echo json_encode(['status'=>'error','message'=>'Cannot delete: timetable has allocations']); exit; }

    $stmt=$pdo->prepare('SELECT * FROM exam_timetable WHERE timetable_id=?');
    $stmt->execute([$id]);
    $before=$stmt->fetch(PDO::FETCH_ASSOC);
    if(!$before){ echo json_encode(['status'=>'error','message'=>'Not found']); exit; }

    $pdo->prepare('DELETE FROM exam_timetable WHERE timetable_id=?')->execute([$id]);
    write_audit_log($pdo,[
        'module'=>'Exam Timetable','action'=>'delete','entity_table'=>'exam_timetable','entity_id'=>$id,
        'before_data'=>$before,'message'=>'Exam timetable deleted'
    ]);
    echo json_encode(['status'=>'success','message'=>'Exam timetable deleted']);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
?>



PHP File Manager