-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedit.php
148 lines (117 loc) · 4.9 KB
/
edit.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
include 'header.php';
// Function to get alumni details by ID
// Function to get alumni details by ID
function getAlumniById($conn, $alumniId) {
$stmt = mysqli_prepare($conn, "SELECT * FROM alumni_table WHERE id = ?");
mysqli_stmt_bind_param($stmt, "i", $alumniId);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
return $result ? mysqli_fetch_assoc($result) : false;
}
// Function to update alumni information
function updateAlumni($conn, $alumniId, $data) {
var_dump($data);
$stmt = mysqli_prepare($conn, "UPDATE alumni_table SET
name = ?,
email = ?,
gradYear = ?,
major = ?,
occupation = ?,
profilePicture = ?
WHERE id = ?");
mysqli_stmt_bind_param($stmt, "ssisssi", $data['name'], $data['email'], $data['gradYear'], $data['major'], $data['occupation'], $data['profilePicture'], $alumniId);
return mysqli_stmt_execute($stmt);
}
// Get the alumni ID from the URL parameter
if(isset($_GET['id'])){
$alumniId = (int)$_GET['id'];
}
// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the existing alumni data
var_dump($_POST);
$alumni = getAlumniById($conn, $alumniId);
if ($alumni) {
// Get the updated values from the form
$alumniId = $_POST['alumniId'];
$updatedData = [
'name' => $_POST['name'],
'email' => $_POST['email'],
'gradYear' => $_POST['gradYear'],
'major' => $_POST['major'],
'occupation' => $_POST['occupation'],
'profilePicture' => $alumni['profilePicture'], // Default to the existing picture
];
// Check if a new profile picture is uploaded
if ($_FILES['profilePicture']['error'] === UPLOAD_ERR_OK) {
$uploadDir = 'uploads/';
$uploadFile = $uploadDir . basename($_FILES['profilePicture']['name']);
if (move_uploaded_file($_FILES['profilePicture']['tmp_name'], $uploadFile)) {
$updatedData['profilePicture'] = $uploadFile;
} else {
echo "Error uploading profile picture.";
}
}
// Update the alumni record in the database
if (updateAlumni($conn, $alumniId, $updatedData)) {
echo "Alumni information updated successfully!";
header('Location: dashboard.php');
exit();
} else {
echo "Error updating alumni information.";
}
} else {
echo "Alumni not found.";
exit();
}
} else {
// Check if the ID is set before querying the database
if ($alumniId !== null) {
// Query to retrieve alumni information based on ID
$alumni = getAlumniById($conn, $alumniId);
// Debugging statement
var_dump($alumni);
if (!$alumni) {
echo "Alumni not found.";
exit();
}
} else {
// Redirect to the dashboard if ID is not set
header('Location: dashboard.php');
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="edit.css">
<title>Edit Alumni</title>
</head>
<body>
<div class="container">
<h1>Edit Alumni</h1>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" enctype="multipart/form-data">
<!-- Include your form fields here with pre-filled values from $alumni -->
<input type="hidden" name="alumniId" value="<?php echo isset($alumni['id']) ? (int)$alumni['id'] : 0; ?>">
<label for="name">Full Name:</label>
<input type="text" id="name" name="name" value="<?php echo $alumni['name']; ?>" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" value="<?php echo $alumni['email']; ?>" required>
<label for="gradYear">Graduation Year:</label>
<input type="number" id="gradYear" name="gradYear" value="<?php echo $alumni['gradYear']; ?>" required>
<label for="major">Major:</label>
<input type="text" id="major" name="major" value="<?php echo $alumni['major']; ?>" required>
<label for="occupation">Occupation:</label>
<input type="text" id="occupation" name="occupation" value="<?php echo $alumni['occupation']; ?>" required>
<label for="profilePicture">Profile Picture:</label>
<input type="file" id="profilePicture" name="profilePicture" accept="image/*">
<button type="submit" name="update">Save Changes</button>
</form>
</div>
<?php include 'footer.php'; ?>
</body>
</html>