File "update.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/exam_timetable/update.php
File size: 1.95 KiB (1994 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; }

    $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; }

    $exam_subject_id = (int)($_POST['exam_subject_id'] ?? 0);
    $exam_date = trim($_POST['exam_date'] ?? '');
    $session = trim($_POST['session'] ?? '');
    $start_time = trim($_POST['start_time'] ?? '');
    $end_time = trim($_POST['end_time'] ?? '');

    $updates=[]; $params=[];
    if($exam_subject_id>0){ $updates[]='exam_subject_id=?'; $params[]=$exam_subject_id; }
    if($exam_date!==''){ $updates[]='exam_date=?'; $params[]=$exam_date; }
    if($session!==''){ $updates[]='session=?'; $params[]=$session; }
    if($start_time!==''){ $updates[]='start_time=?'; $params[]=$start_time; }
    if($end_time!==''){ $updates[]='end_time=?'; $params[]=$end_time; }
    if(!$updates){ echo json_encode(['status'=>'error','message'=>'No fields to update']); exit; }
    $params[]=$id;
    $pdo->prepare('UPDATE exam_timetable SET '.implode(',',$updates).' WHERE timetable_id=?')->execute($params);

    $stmt=$pdo->prepare('SELECT * FROM exam_timetable WHERE timetable_id=?');
    $stmt->execute([$id]);
    $after=$stmt->fetch(PDO::FETCH_ASSOC);
    write_audit_log($pdo,[
        'module'=>'Exam Timetable','action'=>'update','entity_table'=>'exam_timetable','entity_id'=>$id,
        'before_data'=>$before,'after_data'=>$after,'message'=>'Exam timetable updated'
    ]);
    echo json_encode(['status'=>'success','message'=>'Exam timetable updated']);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
?>



PHP File Manager