我是PHP新手,正在从事家庭作业。当我单击<aside>
中的链接时,数据应该更新了。单击该链接只会更新地址栏中的查询参数。查询参数可以很好地更新,但数据保持不变。
以下是相关代码:
// index.php
<?php
require_once('database.php');
// Get category ID
$course_id = filter_input(INPUT_GET, 'course_id',
FILTER_VALIDATE_INT);
if ($course_id == NULL || $course_id == FALSE) {
$course_id = 'cs601';
}
// Get name for selected category
$queryCourse = 'SELECT * FROM sk_courses WHERE courseID = :course_id';
$queryCourseStatement = $db->prepare($queryCourse);
$queryCourseStatement->bindValue(':course_id', $course_id);
$queryCourseStatement->execute();
$course = $queryCourseStatement->fetch();
$course_name = $course['courseName'];
$queryCourseStatement->closeCursor();
// Get all courses
$queryCourses = 'SELECT * FROM sk_courses ORDER BY courseID';
$queryCoursesStatement = $db->prepare($queryCourses);
$queryCoursesStatement->execute();
$courses = $queryCoursesStatement->fetchAll();
$queryCoursesStatement->closeCursor();
// Get products for selected category
$queryStudents = 'SELECT * FROM sk_students WHERE courseID = :course_id ORDER BY studentID';
$queryStudentsStatement = $db->prepare($queryStudents);
$queryStudentsStatement->bindValue(':course_id', $course_id);
$queryStudentsStatement->execute();
$students = $queryStudentsStatement->fetchAll();
$queryStudentsStatement->closeCursor();
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>MMelvin HW5</title>
</head>
<body>
<main class="container">
<h1 class="display-6">Course Manager</h1>
<hr>
<div class="d-flex justify-content-between">
<aside>
<h2 class="display-6">Courses</h2>
<nav>
<ul class="list-group">
<?php foreach ($courses as $course) : ?>
<li class="list-group-item">
<a href="?course_id=<?php echo $course['courseID']; ?>">
<?php echo $course['courseID']; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</nav>
</aside>
<section>
<h2 class="display-6"><?php echo $course_id . ' - ' . $course_name; ?></h2>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th class="text-center">First Name</th>
<th class="text-center">Last Name</th>
<th class="text-center">Email</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ($students as $student) : ?>
<tr>
<td class="text-center"><?php echo $student['firstName']; ?></td>
<td class="text-center"><?php echo $student['lastName']; ?></td>
<td class="text-center"><?php echo $student['email']; ?></td>
<td class="text-center">
<form action="delete_student.php" method="post">
<input class="btn btn-danger" type="submit" name="student_id"
value="Delete">
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p><a href="add_student_form.php">Add Student</a></p>
<p><a href="course_list.php">List courses</a></p>
</section>
</div>
</main>
</body>
</html>
// database.php
<?php
$dsn = 'mysql:host=localhost;dbname=cs602';
$username = 'cs602*********';
$password = '************';
try {
$db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
$error_message = $e->getMessage();
include('../../../MAMP/logs/mysql_error_log.err');
exit();
}
下面是一些屏幕截图,可以更好地说明正在发生的事情。
在初始页面加载时:
点击cs602之后:
这些框应已使用数据库中的相关信息进行更新。
在此先感谢您的帮助。