π File Manager
π /
/
home
/
u857492117
/
domains
/
mangaldaicollege.org
/
public_html
/
app
/
Teacher
Dosya DΓΌzenle: uploadStudents.php
<?php session_start(); include "db.php"; if (!isset($_SESSION['user_id']) || $_SESSION['role'] != "teacher") { header("Location: ../login.php"); exit(); } if (!$conn) { header("Location: studentDataCsv.php?error=db_error"); exit(); } $stream = trim($_POST['stream'] ?? ''); $semester = trim($_POST['semester'] ?? ''); $session = trim($_POST['session'] ?? ''); if (!$stream || !$semester || !$session) { header("Location: studentDataCsv.php?error=missing_fields"); exit(); } // ββ Get uploading teacher's department ββββββββββββββββ // This becomes the department for all students in this upload $teacher_user_id = (int)$_SESSION['user_id']; $td = $conn->prepare("SELECT department FROM teachers WHERE user_id = ? LIMIT 1"); $td->bind_param("i", $teacher_user_id); $td->execute(); $teacher_row = $td->get_result()->fetch_assoc(); $td->close(); $department = $teacher_row['department'] ?? $stream; // fallback to stream if no dept // ββ Ensure department column exists ββββββββββββββββββ $conn->query("ALTER TABLE students ADD COLUMN IF NOT EXISTS department VARCHAR(100) DEFAULT NULL AFTER semester"); if (!isset($_FILES['csv_file']) || $_FILES['csv_file']['error'] !== UPLOAD_ERR_OK) { header("Location: studentDataCsv.php?error=file_error"); exit(); } $ext = strtolower(pathinfo($_FILES['csv_file']['name'], PATHINFO_EXTENSION)); if ($ext !== 'csv') { header("Location: studentDataCsv.php?error=not_csv"); exit(); } $handle = fopen($_FILES['csv_file']['tmp_name'], 'r'); if (!$handle) { header("Location: studentDataCsv.php?error=file_read"); exit(); } // Read header & strip BOM $header_raw = fgetcsv($handle); $header = array_map(function($h) { $h = preg_replace('/^\xEF\xBB\xBF/', '', $h); return strtolower(trim($h)); }, $header_raw); $required = ['name', 'roll_no', 'password']; foreach ($required as $c) { if (!in_array($c, $header)) { fclose($handle); header("Location: studentDataCsv.php?error=missing_column&col=$c"); exit(); } } $col = array_flip($header); $inserted = 0; $skipped = 0; $errors = []; $row_num = 1; while (($row = fgetcsv($handle)) !== false) { $row_num++; if (empty(array_filter($row))) continue; $name = trim($row[$col['name']] ?? ''); $roll_no = trim($row[$col['roll_no']] ?? ''); $password = trim($row[$col['password']] ?? ''); $email = ''; if (isset($col['email'])) $email = trim($row[$col['email']] ?? ''); if (!$email) $email = strtolower(preg_replace('/\s+/', '', $roll_no)) . '@mconnect.local'; $phone = isset($col['phone']) ? trim($row[$col['phone']] ?? '') : ''; if (!$name || !$roll_no || !$password) { $skipped++; continue; } // STEP 1: users table $check = $conn->prepare("SELECT id FROM users WHERE email = ?"); $check->bind_param("s", $email); $check->execute(); $existing_user = $check->get_result()->fetch_assoc(); $check->close(); if ($existing_user) { $user_id = $existing_user['id']; } else { $hashed = password_hash($password, PASSWORD_DEFAULT); $role = 'student'; $ins_user = $conn->prepare("INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, ?)"); $ins_user->bind_param("ssss", $name, $email, $hashed, $role); if (!$ins_user->execute()) { $errors[] = $ins_user->error; $ins_user->close(); $skipped++; continue; } $user_id = $conn->insert_id; $ins_user->close(); } // STEP 2: students table β include department from teacher $ins_student = $conn->prepare( "INSERT INTO students (user_id, name, roll_no, email, phone, stream, semester, session, department) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE user_id=VALUES(user_id), name=VALUES(name), email=VALUES(email), phone=VALUES(phone), stream=VALUES(stream), semester=VALUES(semester), session=VALUES(session), department=VALUES(department)" ); $ins_student->bind_param("issssssss", $user_id, $name, $roll_no, $email, $phone, $stream, $semester, $session, $department ); if ($ins_student->execute()) { $inserted++; } else { $errors[] = $ins_student->error; $skipped++; } $ins_student->close(); } fclose($handle); $error_count = count($errors); header("Location: studentDataCsv.php?success=1&inserted=$inserted&skipped=$skipped&errors=$error_count"); exit(); ?>
πΎ Kaydet
Δ°ptal