35867-vm/update_grade.php
Flatlogic Bot 6d48e88ec8 V1.0
2025-11-20 11:10:15 +00:00

49 lines
1.4 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: index.php');
exit;
}
require_once 'db/config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = $_POST['id'] ?? null;
$grade_name = $_POST['grade_name'] ?? '';
$min_score = $_POST['min_score'] ?? '';
$max_score = $_POST['max_score'] ?? '';
// Basic validation
if (empty($id) || empty($grade_name) || !is_numeric($min_score) || !is_numeric($max_score)) {
$_SESSION['error_message'] = 'All fields are required and scores must be numbers.';
header('Location: school_settings.php');
exit;
}
if ($min_score >= $max_score) {
$_SESSION['error_message'] = 'Min score must be less than max score.';
header('Location: school_settings.php');
exit;
}
try {
$pdo = db();
$sql = "UPDATE grading_scales SET grade_name = ?, min_score = ?, max_score = ? WHERE id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$grade_name, $min_score, $max_score, $id]);
$_SESSION['success_message'] = 'Grade updated successfully.';
} catch (PDOException $e) {
if ($e->errorInfo[1] == 1062) { // Duplicate entry
$_SESSION['error_message'] = "A grade with that name already exists in this section.";
} else {
$_SESSION['error_message'] = 'Database error: ' . $e->getMessage();
}
}
}
header('Location: school_settings.php');
exit;