File "update.php"

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

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

    $fields = ['exam_subject_id','setter_id','status','time_duration','approver_id','approved_at','final_pattern_id'];
    $updates = [];
    $params = [];
    foreach ($fields as $f) {
        if (isset($_POST[$f])) { $updates[] = "$f = ?"; $params[] = $_POST[$f] === '' ? null : $_POST[$f]; }
    }
    if (!$updates) { echo json_encode(['status'=>'error','message'=>'No fields to update']); exit; }
    $params[] = $order_id;

    $sql = 'UPDATE question_paper_orders SET ' . implode(', ', $updates) . ' WHERE order_id = ?';
    $pdo->prepare($sql)->execute($params);

    $stmt = $pdo->prepare('SELECT * FROM question_paper_orders WHERE order_id = ?');
    $stmt->execute([$order_id]);
    $after = $stmt->fetch(PDO::FETCH_ASSOC);

    write_audit_log($pdo, [
        'module' => 'Question Papers',
        'action' => 'update',
        'entity_table' => 'question_paper_orders',
        'entity_id' => $order_id,
        'before_data' => $before,
        'after_data' => $after,
        'message' => 'Question paper order updated'
    ]);

    echo json_encode(['status'=>'success','message'=>'Order updated']);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
?>


PHP File Manager