lis-458-ol-php/grade.php

85 lines
3.3 KiB
PHP

<?php # Script 9.5 - register.php #2
// This script performs an INSERT query to add a record to the users table.
$page_title = 'Course Grade';
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 email address:
if (empty($_POST['last_name'])) { //$_POST is a global variable. empty() method determines whether a variable is considered to be empty.
$errors[] = 'You forgot to enter the last name';
} else {
$ln = mysqli_real_escape_string($dbc, trim($_POST['last_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 CONCAT (StuLastName, ', ', StuFirstName) AS name, CouLongName AS course,CourseGrades AS grade FROM Students, Courses, StudentClass, ScheduleOfClasses
WHERE Students.StudentID=StudentClass.StudentID AND
Courses.CourseID=ScheduleOfClasses.CourseID AND
ScheduleOfClasses.ScheduleID=StudentClass.ScheduleID AND
StuLastName='$ln'";
$r = @mysqli_query ($dbc, $q); // Run the query.
$num = mysqli_num_rows($r); //return the number of rows selected
if ($num > 0) { // If it ran OK, display the records.
// Print how many users there are:
echo "<p>There is the information for the student you are looking for.</p>\n";
// Table header.
echo '<table align="center" cellspacing="3" cellpadding="3" width="75%">
<tr><td align="left"><b>Student</b></td><td align="left"><b>Course</b></td><td align="left"><b>Grade</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['name'] . '</td><td align="left">' . $row['course'] . '</td><td align="left">' . $row['grade'] . '</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 student match with the information you provided</p>';
}
}
mysqli_close($dbc); // Close the database connection.
} // End of the main Submit conditional.
?>
<form class="form-horizontal" action="grade.php" method="post">
<fieldset>
<legend>Search Course Grade by Student Last Name</legend>
<div class="form-group">
<label for="last_name" class="col-lg-2 control-label">Student Last Name</label>
<div class="col-lg-10">
<input class="form-control" placeholder="Last Name" autocomplete="off" type="text" name="last_name" id="last_name" size="15" maxlength="20" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" />
</div>
</div>
<!-- submit button -->
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" name="submit" class="btn btn-primary">Search Course Grade</button>
</div>
</div>
</fieldset>
</form>
<?php include ('includes/footer.html'); ?>