lis-458-ol-php/show_roster.php

71 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2018-02-18 01:28:11 +00:00
<?php # Script 9.5 - ClassRoster.php #2
// This script performs an INSERT query to add a record to the users table.
$page_title = 'Class Roster';
include ('includes/header.html');
// Check for form submission:
// echo $_SERVER['REQUEST_METHOD'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') { //remember the difference between post and get?
require ('./mysqli_connect.php'); // Connect to the db.
$errors = array(); // Initialize an error array.
// Check for an courese information:
if (empty($_POST['course_name'])) { //$_POST is a global variable. empty() method determines whether a variable is considered to be empty.
$errors[] = 'You forgot to enter the course name';
} else {
$fn = mysqli_real_escape_string($dbc, trim($_POST['course_name'])); //mysqli_real_escape_strin()escapes special characters in a string for use in an SQL statement.
}
if (empty($errors)) { // If there is no errors. If everything's OK.
// Make the query:
$q = "SELECT CouLongName, CONCAT(SemesterName, Semesters.SemesterYear) as SemesterInf, CONCAT(StuLastName, ', ', StuFirstName) as name from Students, Semesters, Courses, StudentClass, ScheduleOfClasses
WHERE Students.StudentID=StudentClass.StudentID AND
ScheduleOfClasses.CourseID=Courses.CourseID AND
ScheduleOfClasses.ScheduleID=StudentClass.ScheduleID AND
ScheduleOfClasses.SemesterID=Semesters.SemesterID AND
CouLongName='$fn'";
$r = @mysqli_query ($dbc, $q); // Run the query.
$num = mysqli_num_rows($r);
if ($num > 0) { // If it ran OK, display the records.
// Print how many users there are:
echo "<p>There is the information for the course you are looking for.</p>\n";
// Table header.
echo '<table align="center" cellspacing="3" cellpadding="3" width="75%">
<tr><td align="left"><b>Course Name</b></td><td align="left"><b>Semester</b></td><td align="left"><b>Student Name</b></td></tr>';
// Fetch and print all the records:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { //MYSQLI_ASSOC makes the returned array assortative.
echo '<tr><td align="left">' . $row['CouLongName'] . '</td><td align="left">' . $row['SemesterInf'] . '</td><td align="left">' . $row['name'] . '</td></tr>
';
}
echo '</table>'; // Close the table.
}
else { // If it did not run OK.
// Public message:
echo '<h1>Error</h1>
<p class="error">There is no course roster match with the information you provided</p>';
}
}
mysqli_close($dbc); // Close the database connection.
} // End of the main Submit conditional.
?>
<h1>Class Roster</h1>
<form action="ClassRoster.php" method="post">
<p>Course Name <input type="text" name="course_name" size="15" maxlength="50" value="<?php if (isset($_POST['course_name'])) echo $_POST['course_name']; ?>" /></p>
<p><input type="submit" name="submit" value="Show Class Roster" /></p>
</form>
<?php include ('includes/footer.html'); ?>