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

49 lines
1.5 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') {
$section = $_POST['section'] ?? '';
$grade_name = $_POST['grade_name'] ?? '';
$min_score = $_POST['min_score'] ?? '';
$max_score = $_POST['max_score'] ?? '';
// Basic validation
if (empty($section) || 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 = "INSERT INTO grading_scales (section, grade_name, min_score, max_score) VALUES (?, ?, ?, ?)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$section, $grade_name, $min_score, $max_score]);
$_SESSION['success_message'] = 'Grade added successfully.';
} catch (PDOException $e) {
if ($e->errorInfo[1] == 1062) { // Duplicate entry
$_SESSION['error_message'] = "A grade with the name '{$grade_name}' already exists in the '{$section}' section.";
} else {
$_SESSION['error_message'] = 'Database error: ' . $e->getMessage();
}
}
}
header('Location: school_settings.php');
exit;