-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit_subjects.php
77 lines (65 loc) · 2.9 KB
/
submit_subjects.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
<?php
// Database connection details
session_start();
include('connection/dbconfig.php');
// Create DB Connection
$conn = mysqli_connect($host, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validate and sanitize input (you can expand this as needed)
$subjects = $_POST['SubjectName'];
$semesters = $_POST['Sem'];
$subjectCodes = $_POST['SubjectCode'];
$totalIAMarks = $_POST['totalIAMarks'];
$numInternals = $_POST['numberOfInternals'];
$maxMarksInternals = $_POST['maxMarksOfInternal'];
$theoryIAMarks = $_POST['theoryIA'];
$miniProjectMarks = $_POST['miniProject'];
$avg=$_POST['avg'];
// Prepare and bind SQL statement to check if subject code exists
$checkStmt = $conn->prepare("SELECT COUNT(*) AS count FROM Subjects WHERE sub_code = ?");
$checkStmt->bind_param("s", $sub_code_check);
// Prepare and bind SQL statement to insert new subject
$insertStmt = $conn->prepare("INSERT INTO Subjects (semester, sub_code, sub_name, total_marks, num_internals, max_marks_each_internal, theory_ia_marks, mini_project_marks,avg) VALUES (?, ?, ?, ?, ?, ?, ?, ?,?)");
$insertStmt->bind_param("isssiiiii", $semester, $sub_code, $sub_name, $total_marks, $num_internals, $max_marks_each_internal, $theory_ia_marks, $mini_project_marks,$avg);
// Insert each subject into the database
foreach ($subjects as $key => $subject) {
$semester = $semesters[$key];
$sub_code = $subjectCodes[$key];
$sub_name = $subject;
$total_marks = $totalIAMarks[$key];
$num_internals = $numInternals[$key];
$max_marks_each_internal = $maxMarksInternals[$key];
$theory_ia_marks = $theoryIAMarks[$key];
$mini_project_marks = $miniProjectMarks[$key];
$avg =$avg[$key];
// Check if subject code already exists
$sub_code_check = $sub_code;
$checkStmt->execute();
$result = $checkStmt->get_result();
$row = $result->fetch_assoc();
if ($row['count'] > 0) {
echo "Subject with Subject Code $sub_code already exists. Skipping insertion.<br>";
continue; // Skip insertion if subject code exists
}
// Insert new subject into the database
if (!$insertStmt->execute()) {
echo "Error inserting subject: " . $insertStmt->error;
break; // Exit the loop on first failure
}
}
// Close statements and connection
$checkStmt->close();
$insertStmt->close();
$conn->close();
// Redirect or display success message
header("Location: view_inserted_data.php");
exit();
} else {
echo "Form submission method not allowed.";
}
?>