0) { $sql = "SELECT s.*, g.name as gender_name, c.code as course_code, c.name as course_name, d.code as department_code, d.name as department_name, sc.code as school_code, sc.name as school_name FROM students s LEFT JOIN genders g ON s.gender_id = g.id LEFT JOIN courses c ON s.course_id = c.id LEFT JOIN departments d ON s.department_id = d.id LEFT JOIN schools sc ON s.school_id = sc.id WHERE s.id = ?"; $stmt = mysqli_prepare($conn, $sql); mysqli_stmt_bind_param($stmt, 'i', $student_id); mysqli_stmt_execute($stmt); $result = mysqli_stmt_get_result($stmt); if ($result && mysqli_num_rows($result) > 0) { $student = mysqli_fetch_assoc($result); } else { $_SESSION['message'] = 'Student not found!'; $_SESSION['message_type'] = 'danger'; header('Location: manage_students.php'); exit(); } mysqli_stmt_close($stmt); } else { $_SESSION['message'] = 'Invalid student ID!'; $_SESSION['message_type'] = 'danger'; header('Location: manage_students.php'); exit(); } // Get dropdown data $genders = []; $courses = []; $departments = []; $schools = []; // Genders $result = mysqli_query($conn, "SELECT * FROM genders ORDER BY id"); while ($row = mysqli_fetch_assoc($result)) { $genders[] = $row; } // Courses $result = mysqli_query($conn, "SELECT * FROM courses WHERE status = 1 ORDER BY code"); while ($row = mysqli_fetch_assoc($result)) { $courses[] = $row; } // Departments $result = mysqli_query($conn, "SELECT * FROM departments WHERE status = 1 ORDER BY code"); while ($row = mysqli_fetch_assoc($result)) { $departments[] = $row; } // Schools $result = mysqli_query($conn, "SELECT * FROM schools WHERE status = 1 ORDER BY code"); while ($row = mysqli_fetch_assoc($result)) { $schools[] = $row; } // Handle form submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_student'])) { // Prefer POSTed id if present $student_id = isset($_POST['id']) ? intval($_POST['id']) : $student_id; $student_data = [ 'student_id' => mysqli_real_escape_string($conn, trim($_POST['student_id'])), 'full_name' => mysqli_real_escape_string($conn, trim($_POST['full_name'])), 'gender_id' => intval($_POST['gender_id']), 'year_level' => intval($_POST['year_level']), 'course_id' => intval($_POST['course_id']), 'department_id' => intval($_POST['department_id']), 'school_id' => intval($_POST['school_id']), 'birth_date' => (isset($_POST['birth_date']) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $_POST['birth_date']) && $_POST['birth_date'] !== '0000-00-00') ? mysqli_real_escape_string($conn, $_POST['birth_date']) : NULL, 'contact_number' => mysqli_real_escape_string($conn, trim($_POST['contact_number'])), 'email' => mysqli_real_escape_string($conn, trim($_POST['email'])), 'address' => mysqli_real_escape_string($conn, trim($_POST['address'])), 'status' => isset($_POST['status']) ? 1 : 0 ]; // Validate required fields $required_fields = ['student_id', 'full_name', 'gender_id', 'year_level', 'course_id', 'department_id', 'school_id']; $valid = true; foreach ($required_fields as $field) { if (empty($student_data[$field])) { $valid = false; $message = "Missing required field: $field"; $message_type = 'danger'; break; } } // Validate email format if provided if ($valid && !empty($student_data['email']) && !filter_var($student_data['email'], FILTER_VALIDATE_EMAIL)) { $valid = false; $message = 'Please enter a valid email address.'; $message_type = 'danger'; } // Check for duplicate student ID (excluding current student) if ($valid) { $check_sql = "SELECT id FROM students WHERE student_id = ? AND id != ?"; $stmt = mysqli_prepare($conn, $check_sql); mysqli_stmt_bind_param($stmt, 'si', $student_data['student_id'], $student_id); mysqli_stmt_execute($stmt); mysqli_stmt_store_result($stmt); if (mysqli_stmt_num_rows($stmt) > 0) { $message = 'Student ID already exists. Please use a different Student ID.'; $message_type = 'danger'; $valid = false; } mysqli_stmt_close($stmt); } if ($valid) { // Build SQL query allowing NULLs for optional fields $setParts = [ 'student_id = ?', 'full_name = ?', 'gender_id = ?', 'year_level = ?', 'course_id = ?', 'department_id = ?', 'school_id = ?', 'status = ?', 'updated_at = NOW()' ]; $types = 'ssiiiiii'; $params = [ $student_data['student_id'], $student_data['full_name'], $student_data['gender_id'], $student_data['year_level'], $student_data['course_id'], $student_data['department_id'], $student_data['school_id'], $student_data['status'] ]; // Optional fields: set to NULL when empty if (!empty($student_data['birth_date'])) { $setParts[] = 'birth_date = ?'; $types .= 's'; $params[] = $student_data['birth_date']; } else { $setParts[] = 'birth_date = NULL'; } if (!empty($student_data['contact_number'])) { $setParts[] = 'contact_number = ?'; $types .= 's'; $params[] = $student_data['contact_number']; } else { $setParts[] = 'contact_number = NULL'; } if (!empty($student_data['email'])) { $setParts[] = 'email = ?'; $types .= 's'; $params[] = $student_data['email']; } else { $setParts[] = 'email = NULL'; } if (!empty($student_data['address'])) { $setParts[] = 'address = ?'; $types .= 's'; $params[] = $student_data['address']; } else { $setParts[] = 'address = NULL'; } $sql = 'UPDATE students SET ' . implode(', ', $setParts) . ' WHERE id = ?'; $types .= 'i'; $params[] = $student_id; $stmt = mysqli_prepare($conn, $sql); if (!$stmt) { $message = 'Error preparing update: ' . mysqli_error($conn); $message_type = 'danger'; } else { mysqli_stmt_bind_param($stmt, $types, ...$params); if (mysqli_stmt_execute($stmt)) { $_SESSION['message'] = 'Student updated successfully!'; $_SESSION['message_type'] = 'success'; header("Location: view_student.php?id=$student_id"); exit(); } else { $error_msg = mysqli_error($conn); $message = 'Error updating student: ' . $error_msg; $message_type = 'danger'; } mysqli_stmt_close($stmt); } } } include '../includes/header.php'; ?>

Edit Student

Back to Students
Student Information
Unique student identification number
>
Cancel
Student Summary
Student Photo

Active Inactive
Gender
Course
Year Level Year
Department
School

Student Not Found

The student you're trying to edit does not exist or has been deleted.

Back to Students List
function validateEditForm() { console.log("Validating form..."); // Get form values const studentId = document.getElementById("student_id_field").value.trim(); const fullName = document.getElementById("full_name_field").value.trim(); const genderId = document.getElementById("gender_id_field").value; const yearLevel = document.getElementById("year_level_field").value; const schoolId = document.getElementById("school_id_field").value; const departmentId = document.getElementById("department_id_field").value; const courseId = document.getElementById("course_id_field").value; const email = document.getElementById("email_field").value.trim(); // Check required fields if (!studentId) { alert("Please enter Student ID"); document.getElementById("student_id_field").focus(); return false; } if (!fullName) { alert("Please enter Full Name"); document.getElementById("full_name_field").focus(); return false; } if (!genderId) { alert("Please select Gender"); document.getElementById("gender_id_field").focus(); return false; } if (!yearLevel) { alert("Please select Year Level"); document.getElementById("year_level_field").focus(); return false; } if (!schoolId) { alert("Please select School"); document.getElementById("school_id_field").focus(); return false; } if (!departmentId) { alert("Please select Department"); document.getElementById("department_id_field").focus(); return false; } if (!courseId) { alert("Please select Course"); document.getElementById("course_id_field").focus(); return false; } // Validate email format if provided if (email && !isValidEmail(email)) { alert("Please enter a valid email address"); document.getElementById("email_field").focus(); return false; } // Show loading state document.getElementById("submitBtn").innerHTML = \' Updating...\'; document.getElementById("submitBtn").disabled = true; console.log("Form validation passed"); return true; } function isValidEmail(email) { const emailRegex = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/; return emailRegex.test(email); } // Debug form submission document.getElementById("editStudentForm").addEventListener("submit", function(e) { console.log("Form submitted"); console.log("Form action:", this.action); console.log("Form method:", this.method); const formData = new FormData(this); console.log("Form data:"); for (let [key, value] of formData.entries()) { console.log(key + ": " + value); } }); // Check if form elements exist document.addEventListener("DOMContentLoaded", function() { console.log("DOM loaded"); console.log("Form exists:", document.getElementById("editStudentForm") !== null); console.log("Submit button exists:", document.getElementById("submitBtn") !== null); // Test form submission via JavaScript document.getElementById("editStudentForm").onsubmit = function() { console.log("Form onsubmit fired"); return validateEditForm(); }; }); '; include '../includes/footer.php'; ?>