35296-vm/import_students.php
Flatlogic Bot 8d0660a262 yu
2025-11-02 07:44:15 +00:00

44 lines
1.2 KiB
PHP

<?php
session_start();
if (!isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin') {
header('Location: index.php');
exit;
}
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/db/config.php';
use ParseCsv\Csv;
if (isset($_FILES['student_file']) && $_FILES['student_file']['error'] === UPLOAD_ERR_OK) {
$csv = new Csv();
$csv->auto($_FILES['student_file']['tmp_name']);
$pdo = db();
foreach ($csv->data as $row) {
$full_name = $row['full_name'];
$email = $row['email'];
// Generate a random password
$password = bin2hex(random_bytes(8));
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
try {
$stmt = $pdo->prepare("INSERT INTO users (full_name, email, password, role) VALUES (?, ?, ?, 'student')");
$stmt->execute([$full_name, $email, $hashed_password]);
} catch (PDOException $e) {
// Handle duplicate email or other errors
// For now, we just skip the row
continue;
}
}
header('Location: admin_dashboard.php?import_success=1');
exit;
} else {
header('Location: admin_dashboard.php?import_error=1');
exit;
}