Path : /home/vishqocm/pcib.in/admin/database/
File Upload :
Current File : /home/vishqocm/pcib.in/admin/database/setup_attendance_table.php

<?php
// Include database configuration
require_once __DIR__ . '/db_config.php';

// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Create the attendance table if it doesn't exist
$create_attendance_table_sql = "
CREATE TABLE IF NOT EXISTS `attendance` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `enrollment_id` int(11) NOT NULL,
  `course_id` int(11) NOT NULL,
  `student_id` int(11) NOT NULL,
  `date` date NOT NULL,
  `status` enum('present','absent','late','excused') NOT NULL DEFAULT 'present',
  `marked_by` int(11) DEFAULT NULL,
  `notes` text DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `enrollment_id` (`enrollment_id`),
  KEY `course_id` (`course_id`),
  KEY `student_id` (`student_id`),
  KEY `marked_by` (`marked_by`),
  KEY `date` (`date`),
  CONSTRAINT `attendance_course_id_fk` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE,
  CONSTRAINT `attendance_enrollment_id_fk` FOREIGN KEY (`enrollment_id`) REFERENCES `enrollments` (`id`) ON DELETE CASCADE,
  CONSTRAINT `attendance_student_id_fk` FOREIGN KEY (`student_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `attendance_marked_by_fk` FOREIGN KEY (`marked_by`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
";

// Execute query
if ($conn->query($create_attendance_table_sql) === TRUE) {
    echo "Attendance table created successfully!";
} else {
    echo "Error creating attendance table: " . $conn->error;
}

// Close the connection only if this script is run directly
if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
    $conn->close();
}
?>