PHP新手求助:如何修改代码实现教师查询指定学生测验成绩
Hey there, let's fix this up for you! The core issue here is getting the student username from the form submission correctly and using it to fetch their scores instead of always using the logged-in teacher's username. Here's a step-by-step adapted solution that fits your existing code:
1. Fix the Student Username Input Form
Your original form had the wrong action target, input name, and submission method. Let's update it to submit to the same page (so we can handle the logic in one place) and use POST for better security:
<p>Welcome, teachers! Here you can track student progress.</p> <p>Enter the username of the student you wish to view results for:</p> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>"> Username: <input type="text" name="student_username" placeholder="Enter student username" required><br> <input type="submit" value="View Results"> </form>
method="POST"hides the username from the URL (better for privacy)action="<?php echo $_SERVER['PHP_SELF']; ?>"submits the form to the current pagename="student_username"gives us a clear identifier to fetch the input later- Added
requiredandplaceholderfor better user experience
2. Update the PHP Logic to Handle Form Submission
First, keep your existing login validation, then add code to capture the submitted student username and use it to fetch scores:
<?php session_start(); // Keep your original login check (added exit to stop code if redirecting) if (!isset($_SESSION['username']) || $_SESSION['username'] == '') { header("Location: login.php"); exit; } // Capture the submitted student username (if form was submitted) $target_student = ''; if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['student_username'])) { $target_student = trim($_POST['student_username']); } ?>
3. Modify the Score Table to Use the Target Student's Username
Update your table code to fetch scores for the submitted student (or show a friendly message if no username was entered):
<table class="table table-hover" style="text-align:center;"> <thead> <tr> <th style="text-align:center;">Quiz</th> <th style="text-align:center;">Score (%)</th> <th style="text-align:center;">Certificate</th> <th style="text-align:center;">Retake</th> </tr> </thead> <tbody> <?php if (!empty($target_student)) { $a = new NewQuizScore; $scores = $a->getScores($target_student); if (!empty($scores)) { foreach ($scores as $score) { // Use htmlspecialchars to prevent XSS attacks (critical for security!) echo "<tr>"; echo "<td>" . htmlspecialchars($score[0]) . "</td>"; echo "<td>" . htmlspecialchars($score[1]) . "</td>"; // Use urlencode for link parameters to avoid broken links with special characters echo '<td><a href="certificate.php?name=' . urlencode($score[0]) . '&score=' . urlencode($score[1]) . '">Print Certificate</a></td>'; echo "<td><a href=\"quiz-show.php?quiz=01_PythonBasics\">Take it again!</a></td>"; echo "</tr>"; } } else { echo "<tr><td colspan='4'>No quiz scores found for this student.</td></tr>"; } } else { echo "<tr><td colspan='4'>Please enter a student username above to view their results.</td></tr>"; } ?> </tbody> </table>
Why Your Previous Attempts Didn't Work
- When you tried using
$_SESSION['username1'], that's incorrect because form submissions don't automatically populate the session—you have to fetch values from$_POSTor$_GETfirst. - If you switched to POST but didn't update the form's
actionto point to the correct page (or forgot to use$_POST['username1']instead of the session variable), the code wouldn't pick up the input.
A quick extra tip: If your system needs to restrict access (only teachers can view student scores), make sure your NewQuizScore::getScores method or your page logic verifies that the logged-in user is a teacher (to prevent unauthorized access).
内容的提问来源于stack exchange,提问作者Compoot

