File "update.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/categories/update.php
File size: 2.74 KiB (2809 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 {
    $id = (int)($_POST['id'] ?? 0);
    $name = trim($_POST['category_name'] ?? '');
    $desc = trim($_POST['description'] ?? '');
    
    // Validation
    $errors = [];
    if ($id <= 0) { 
        $errors[] = 'Invalid ID.'; 
    }
    if ($name === '') { 
        $errors[] = 'Category name is required.'; 
    }
    if (mb_strlen($name) < 2) { 
        $errors[] = 'Category name must be at least 2 characters.'; 
    }
    if (mb_strlen($name) > 100) { 
        $errors[] = 'Category name must not exceed 100 characters.'; 
    }
    
    if ($errors) { 
        echo json_encode(["status" => "error", "message" => implode(' ', $errors)]); 
        exit; 
    }

    // Check if category exists
    $check_stmt = $pdo->prepare('SELECT * FROM categories WHERE category_id = ?');
    $check_stmt->execute([$id]);
    $existing = $check_stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$existing) {
        echo json_encode(["status" => "error", "message" => "Category not found."]);
        exit;
    }

    // Check for duplicate category name (excluding current record)
    $duplicate_stmt = $pdo->prepare("SELECT category_id FROM categories WHERE category_name = ? AND category_id != ?");
    $duplicate_stmt->execute([$name, $id]);
    if ($duplicate_stmt->fetch()) {
        echo json_encode(["status" => "error", "message" => "Category name already exists. Please use a different name."]);
        exit;
    }

    // Update category
    $stmt = $pdo->prepare("UPDATE categories SET category_name = ?, description = ? WHERE category_id = ?");
    $stmt->execute([$name, $desc, $id]);
    
    // Write audit log with before/after data
    write_audit_log($pdo, [
        'module' => 'Academic Structure',
        'action' => 'update',
        'entity_table' => 'categories',
        'entity_id' => $id,
        'before_data' => $existing,
        'after_data' => [
            'category_name' => $name,
            'description' => $desc
        ],
        'message' => 'Category updated successfully'
    ]);
    
    echo json_encode([
        "status" => "success",
        "message" => "Category updated successfully.",
        "data" => [
            "category_id" => $id,
            "category_name" => $name,
            "description" => $desc
        ]
    ]);
    
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode([
        "status" => "error",
        "message" => "An error occurred while updating the category. Please try again."
    ]);
}
?>

PHP File Manager