-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateCourse.php
69 lines (59 loc) · 2.39 KB
/
updateCourse.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
<?php
// Configuration
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tga";
// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Check if the courseID parameter is set
if (isset($_GET['courseID']) && !empty($_GET['courseID'])) {
$courseID = $_GET['courseID'];
// Display a form to enter the teacher's ID
echo "<form action='updateCourse.php' method='post'>";
echo "Enter Teacher's ID: <input type='text' name='teacherID'><br>";
echo "<input type='hidden' name='courseID' value='$courseID'>";
echo "<input type='submit' value='Update Course'>";
echo "</form>";
// Check if the form has been submitted
if (isset($_POST['teacherID']) && !empty($_POST['teacherID'])) {
$teacherID = $_POST['teacherID'];
$courseID = $_POST['courseID'];
// Check if the course exists
$courseStmt = $mysqli->prepare("SELECT * FROM courses WHERE courseID = ?");
$courseStmt->bind_param("i", $courseID);
$courseStmt->execute();
$courseResult = $courseStmt->get_result();
if ($courseResult->num_rows > 0) {
// Course exists, proceed with update
// Check if the teacherID exists in the teachers table
$teacherStmt = $mysqli->prepare("SELECT * FROM teachers WHERE teacherID = ?");
$teacherStmt->bind_param("i", $teacherID);
$teacherStmt->execute();
$teacherResult = $teacherStmt->get_result();
if ($teacherResult->num_rows > 0) {
// TeacherID exists, proceed with update
$updateStmt = $mysqli->prepare("UPDATE courses SET teacherID = ? WHERE courseID = ?");
$updateStmt->bind_param("ii", $teacherID, $courseID);
$updateStmt->execute();
echo "Course with ID $courseID has been updated successfully.";
} else {
// TeacherID doesn't exist, handle the error
echo "Error: Teacher with ID $teacherID does not exist.";
}
} else {
// Course doesn't exist, handle the error
echo "Error: Course with ID $courseID does not exist.";
}
}
} else {
echo "Error: Course ID is required.";
exit;
}
// Close connection
$mysqli->close();
?>