-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_student_details.php
40 lines (32 loc) · 1.61 KB
/
add_student_details.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
<?php
require_once('./database/connect.php');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-Type');
header('Content-Type: application/json');
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$data = json_decode(file_get_contents('php://input'), true);
if (!is_array($data)) {
echo json_encode(array('status' => 'error', 'message' => 'Invalid data format. Expected an array.'));
exit;
}
try {
$stmt = $conn->prepare("INSERT INTO students (registerNumber, name, section, department, specialization, batch, careerOption, facultyAdvisorName) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
foreach ($data as $student) {
$registerNumber = $student["registerNumber"];
$name = $student["name"];
$section = $student["section"];
$department = $student["department"];
$specialization = $student["specialization"];
$batch = $student["batch"];
$careerOption = $student["careerOption"];
$facultyAdvisorName = $student["facultyAdvisorName"];
$stmt->execute([$registerNumber, $name, $section, $department, $specialization, $batch, $careerOption, $facultyAdvisorName]);
}
// Return JSON response for successful insertion
echo json_encode(array('status' => 'success', 'message' => 'Student details added successfully!'));
} catch (PDOException $e) {
// Return JSON response for database error
echo json_encode(array('status' => 'error', 'message' => 'Error: ' . $e->getMessage()));
}
}