PHP Web Application Development: Database CRUD Operations

Verified

Added on  2019/09/26

|7
|1724
|324
Practical Assignment
AI Summary
This assignment comprises several PHP files designed to interact with a database for managing student and course enrollment data. The `readAll.php` file displays all enrollment records, while `select.php` allows users to search for specific records based on course title and student last name. The `createStudent.php` file enables the creation of new student records. `Gradechange.php` facilitates updating student grades, and `Dropcourse.php`, `Dropenrollment.php`, and `Dropstudent.php` handle deleting course, enrollment, and student records respectively. Each file includes HTML forms for user input and PHP code to connect to the database, execute SQL queries (including SELECT, INSERT, UPDATE, and DELETE statements), and display or modify data. The assignment demonstrates fundamental web application development concepts, including database interaction, form handling, and data manipulation using PHP.
Document Page
readAll.php
<html>
<head>
<title>Read all records</title>
</head>
<body>
<form method="post" action="readAll_hint.php">
<input type="submit" value="Show All Enrollments">
</form>
</body>
</html>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
//Code to connect to the database
$query = "SELECT EnrollmentID, course.CourseID, course.Title,
course.Credits,
student.StudentID, student.LastName, student.FirstName, Grade,
EnrollmentSemester
FROM enrollment
INNER JOIN course ON course.CourseID
INNER JOIN student ON student.StudentID
WHERE enrollment.CourseID = course.CourseID AND
enrollment.StudentID = student.StudentID
ORDER BY student.LastName";
//Code to run the query and retrieve necessary data and show
them on the web page
}
?>
Select.php
<html>
<head>
<title>Read selected records</title>
</head>
<body>
<form method="post" action="select_hint.php">
Title: <input type="text" name="courseTitle"><br />
Last Name: <input type="text" name="studentLastName"><br />
<input type="submit" value="Show Enrollments">
</form>
</body>
</html>
<?php
//Define a sanitize function to sanitize user's inputs
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
if(isset($_POST['courseTitle']) && isset($_POST['studentLastName']))
{
//Code to connect to the DB
//Code to sanitize inputs
$query = "SELECT student.LastName, course.Title, Grade,
EnrollmentSemester
FROM enrollment
INNER JOIN course ON course.CourseID
INNER JOIN student ON student.StudentID
WHERE enrollment.CourseID = course.CourseID AND
enrollment.StudentID = student.StudentID
AND course.Title LIKE '%$sanitizedCourseTitle%' AND
student.LastName LIKE '%$sanitizedStudentLastName%'";
//Code to run the query and retrieve necessary data and show
them on the web page.
}
?>
createStudent.php
<html>
<head>
<title>Insert Student</title>
</head>
<body>
<h1>Create new Student</h1>
<form method="post" action="createStudent_sol.php" >
<label for="fname_field">First Name: </label>
<input id="fname_field" type="text" name="firstName" /><br />
<br />
<label for="lname_field">Last Name: </label>
<input id="lname_field" type="text" name="lastName" /><br />
<br />
<input type="submit" value="Add New Student" />
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
//Code to connect to the DB
//Code to retrieve the highest value of Student ID currently
in the DB
if(isset($_POST['firstName']) &&
isset($_POST['lastName']==""))
{
//Santize inputs
$query = "INSERT INTO student VALUES('$studentID',
'$firstName', '$lastName')";
Document Page
//Run the query of insertion
}
//Define a input sanitize function
?>
Gradechange.php
<html>
<head>
<title>Change Grade</title>
</head>
<body>
<h1>Change Grade</h1>
<form method="post" action="gradeChange_sol.php" >
<label for="studentID_field">Student ID: </label>
<input id="studentID_field" type="text" name="studentID" /><br />
<br />
<label for="courseID_field">Course ID: </label>
<input id="courseID_field" type="text" name="courseID" /><br />
<br />
<label for="enrollment_field">Enrollment Semester (format: FA17): </label>
<input id="enrollment_field" type="text" name="enrollmentSemester"
size="4"/><br />
<br />
<label for="grade_field">New Grade: </label>
<input id="grade_field" type="text" name="newGrade" size="3"/><br />
<br />
<input type="submit" value="Update Grade" />
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
//Code to connect to DB
if(isset($_POST['studentID']) && isset($_POST['courseID']=="")
&& isset($_POST['enrollmentSemester']=="") && isset($_POST['newGrade']==""))
{
//Code to sanitize inputs
$query = "UPDATE enrollment SET Grade='$newGrade' WHERE
StudentID='$studentID' AND CourseID='$courseID' AND
EnrollmentSemester='$enrollmentSemester'";
//Code to run the query of UPDATE
}
}
//Define a input sanitize function
Document Page
?>
Dropcourse.php
<?php
if($_SERVER['REQUEST_METHOD']=="POST") {
// Sanitize input
// Code to connect to DB
// Delete the record for the given course ID
$query = "DELETE FROM course WHERE courseID='$courseID'";
// Execute query
}
//Define a sanitize function
}
?>
<html>
<head>
<title>Drop Course</title>
<style>
.error {
color: #FF0000;
font-weight: bold;
}
.success {
color: #009900;
font-weight: bold;
}
</style>
</head>
<body>
<h3>Drop Course Record</h3>
<form action="dropCourse.php" method="post">
<label for="courseID_field">Course ID: </label>
<input type="text" id="courseID_field" name="courseID" size="4"/><br /><br
/>
<input type="submit" value="Delete Record"/>
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
</form>
</body>
</html>
Dropenrollment.php
<?php
if($_SERVER['REQUEST_METHOD']=="POST") {
// Sanitize input
// Code to connect to DB
// Delete the record matching the given Student ID, Course ID, and
EnrollmentSemester
$query = "DELETE FROM enrollment WHERE studentID='$studentID' AND
courseID='$courseID' AND enrollmentSemester='$enrollmentSemester'";
// Execute query
}
//Define sanitize function
?>
<html>
<head>
<title>Drop Enrollment</title>
<style>
.error {
color: #FF0000;
font-weight: bold;
}
.success {
color: #009900;
font-weight: bold;
}
</style>
</head>
<body>
<h3>Drop Enrollment Record</h3>
<form action="" method="post">
<label for="studentID_field">Student ID: </label>
Document Page
<input type="text" id="studentID_field" name="studentID" size="4"/><br
/><br />
<label for="courseID_field">Course ID: </label>
<input type="text" id="courseID_field" name="courseID" size="4"/><br /><br
/>
<label for="enrollmentSemester_field">Enrollment Semester: </label>
<input type="text" id="enrollmentSemester_field" name="enrollmentSemester"
placeholder="FA##" size="4" maxlength="4"/><br /><br />
<input type="submit" value="Delete Record"/>
</form>
</body>
</html>
Dropstudent.php
if($_SERVER['REQUEST_METHOD']=="POST") {
// Sanitize input
// Connect to db
// Delete the record matching the given Student ID, Student First Name
and Last Name
$query = "DELETE FROM student WHERE studentID='$studentID' AND
LastName='$lname' AND FirstName='$fname'";
// Execute query
$result = $conn->query($query);
}
}
}
//Define a sanitize function
?>
<html>
<head>
<title>Drop Student</title>
<style>
.error {
color: #FF0000;
font-weight: bold;
}
.success {
Document Page
color: #009900;
font-weight: bold;
}
</style>
</head>
<body>
<h3>Drop Student Record</h3>
<form action="" method="post">
<label for="studentID_field">Student ID: </label>
<input type="text" id="studentID_field" name="studentID" size="4"/><br
/><br />
<label for="fname_field">First Name: </label>
<input type="text" id="fname_field" name="fname"/><br /><br />
<label for="lname_field">Last Name: </label>
<input type="text" id="lname_field" name="lname" /><br /><br />
<input type="submit" value="Delete Record"/>
</form>
</body>
</html>
chevron_up_icon
1 out of 7
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]