-
Notifications
You must be signed in to change notification settings - Fork 0
/
addGroupOfStudentsTreatment.php
91 lines (84 loc) · 3.32 KB
/
addGroupOfStudentsTreatment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
require_once("connection.php");
function verifyFields($field)
{
// $fieldInput = $_POST[$field];
$fileUpload = basename($_FILES["fileUpload"]["tmp_name"]);
$msgReturn = "";
switch ($field) {
case "fileUpload":
if ($fileUpload == "") {
$msgReturn .= "Please select a file to upload<br>";
}
break;
}
return $msgReturn;
}
function checkError($post)
{
$Error = [];
foreach ($post as $key => $value) {
$err = verifyFields($key);
if (strlen($err) > 0)
$Error[] = $err;
}
return $Error;
}
if (isset($_FILES["fileUpload"])) {
$errors = checkError($_POST);
if (empty($errors)) {
$row = 1;
if (($handle = fopen($_FILES["fileUpload"]["tmp_name"], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ";")) !== FALSE) {
$num = count($data);
// var_dump($data);
if ($row > 1) {
$nom = $data[0];
$telephone = $data[4];
$email = $data[3];
$password = $data[3];
$role = "etudiant";
$classe = filter_input(INPUT_POST, "classe");
if (strlen($nom) > 1) {
$sql = $conn->prepare("INSERT INTO `users`(`id`, `nom`, `telephone`, `email`, `password`, `role`, `classe`) VALUES (NULL, :nom, :telephone, :email, :password, :role, :classe)");
$sql->execute([':nom' => $nom, ':telephone' => $telephone, ':email' => $email, ':password' => $password, ':role' => $role, ':classe' => $classe]);
}
}
$row++;
// for ($i = 0; $i < $num; $i++) {
// echo $data[$i] . "<br />\n";
// }
}
fclose($handle);
}
if (!$conn) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
} else {
// $sql = $conn->prepare("INSERT INTO `users`(`id`, `name`, `email`, `password`, `role`, `classe`) VALUES (NULL, :name, :email, :password, :role, :classe)");
// $sql->execute([':fileupload' => $fileupload]);
// echo ($handle);
// header('Location: addGroupOfStudents.php?id=database_updated');
}
}
}
if (isset($_POST["classe"], $_POST["debut"], $_POST["fin"])) {
$errors = checkError($_POST);
if (empty($errors)) {
$classe = filter_input(INPUT_POST, "classe");
$debut = filter_input(INPUT_POST, "debut");
$fin = filter_input(INPUT_POST, "fin");
if (!$conn) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
} else {
$sql = "INSERT INTO `classes`(`id`, `classe`, `debut_cours`, `fin_cours`) VALUES (NULL,'" . $classe . "','" . $debut . "','" . $fin . "')";
$add = $conn->prepare($sql);
$add->execute();
header('Location: addGroupOfStudents.php?id=database_updated');
}
mysqli_close($conn);
}
}