-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStudent_update.php
More file actions
63 lines (54 loc) · 2.03 KB
/
Copy pathStudent_update.php
File metadata and controls
63 lines (54 loc) · 2.03 KB
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
<?php
header('Content-Type: application/json');
// Database connection
$host = 'localhost';
$dbname = 'school_db';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo json_encode(['error' => 'Database connection failed: ' . $e->getMessage()]);
exit();
}
// Validate input
if (!isset($_POST['student_id']) || !isset($_POST['student_name']) ||
!isset($_POST['student_grade']) || !isset($_POST['parent_student_id']) ||
!isset($_POST['parent_name']) || !isset($_POST['parent_email']) ||
!isset($_POST['parent_telephone'])) {
echo json_encode(['error' => 'Missing required information']);
exit();
}
$student_id = $_POST['student_id'];
$student_name = $_POST['student_name'];
$student_grade = $_POST['student_grade'];
$parent_student_id = $_POST['parent_student_id'];
$parent_name = $_POST['parent_name'];
$parent_email = $_POST['parent_email'];
$parent_telephone = $_POST['parent_telephone'];
try {
// Update student information
$stmt = $pdo->prepare("UPDATE students SET name = :name, grade = :grade WHERE id = :id");
$stmt->execute([
'name' => $student_name,
'grade' => $student_grade,
'id' => $student_id
]);
// Update parent information
$stmt = $pdo->prepare("UPDATE parents_contact SET `parent name` = :parent_name, email = :email, `telephone number` = :telephone WHERE student_id = :student_id");
$stmt->execute([
'parent_name' => $parent_name,
'email' => $parent_email,
'telephone' => $parent_telephone,
'student_id' => $parent_student_id
]);
$auditStmt = $pdo->prepare("INSERT INTO student_audit (student_id, operation) VALUES (:student_id, 'Updated')");
$auditStmt->execute([
'student_id' => $student_id
]);
echo json_encode(['success' => true]);
} catch(PDOException $e) {
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
}
?>