File "create.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/halls/create.php
File size: 1.43 KiB (1469 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 {
    $college_id = (int)($_POST['college_id'] ?? 0);
    $hall_name = trim($_POST['hall_name'] ?? '');
    $block_name = trim($_POST['block_name'] ?? '');
    $capacity = (int)($_POST['capacity'] ?? 0);

    $errors = [];
    if ($college_id <= 0) $errors[] = 'College is required';
    if ($hall_name === '') $errors[] = 'Hall name is required';
    if ($capacity <= 0) $errors[] = 'Capacity must be greater than 0';
    if ($errors) { echo json_encode(['status'=>'error','message'=>implode(', ', $errors)]); exit; }

    $stmt = $pdo->prepare('INSERT INTO halls (college_id, hall_name, block_name, capacity) VALUES (?,?,?,?)');
    $stmt->execute([$college_id, $hall_name, $block_name, $capacity]);
    $hall_id = (int)$pdo->lastInsertId();

    write_audit_log($pdo, [
        'module' => 'Timetable & Halls',
        'action' => 'create',
        'entity_table' => 'halls',
        'entity_id' => $hall_id,
        'after_data' => compact('hall_id','college_id','hall_name','block_name','capacity'),
        'message' => 'Hall created'
    ]);

    echo json_encode(['status'=>'success','message'=>'Hall created successfully','data'=>['hall_id'=>$hall_id]]);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
?>



PHP File Manager