こんにちは、私のデータが複製され、エコーされる原因となっているエラーを3回ではなく3回確認しているようです。もし誰かが私にこの問題を解決するのを助けることができた。データは1回ではなく3回エコーされています
<?php
session_start();
//Connect to database
if(!isset($_SESSION['username'])) { //check if user-name is not given
echo('you are not authorised to veiw this page, log in with admin account to view it');
//show login page
include 'login.html';
}
$con=mysqli_connect("localhost:3307","root","usbw","degreeproject");
//check if query sucessful else report error
$classid = $_GET['classid'];
$studentid = $_GET['userid'];
$sql3 = "SELECT usernames.username, quizzes.QuizID, results.score, results.QuizID,enrolledclasses.UserID, quizzes.ClassID FROM `results`, enrolledclasses, usernames, quizzes
WHERE usernames.username = results.username
AND quizzes.QuizID = results.QuizID
AND enrolledclasses.UserID = $studentid
AND quizzes.ClassID = $classid;";
if (!$result3=mysqli_query($con,$sql3)) {
die('Error: ' . mysqli_error($con));
}
else {
//count the number of records from the query result
$count=mysqli_num_rows($result3);
}
if($count>=1) {
$result3 = mysqli_query($con,$sql3);
while(list($Username, $QuizID, $Score) = mysqli_fetch_row($result3)) {
$sql = "SELECT COUNT(Question) as total FROM questions WHERE QuizID = $QuizID";
$result = mysqli_query($con, $sql);
$numquestions;
if($result->num_rows > 0) {
$numquestions = $result->fetch_assoc()['total'];
}
echo"<table>";
echo "<tr>";
echo "<th>Username: </th>";
echo "<th>QuizID: </th>";
echo "<th>Score: </th>";
echo "<th>Go Back: </th>";
echo "</tr>";
echo "<tr>";
echo("<td>$Username</td>");
echo("<td>$QuizID</td>");
echo("<td>$Score/$numquestions</td>");
echo("<td></td>");
echo "<td><a href ='../CurrentlyBeingWorkedOn/index.php'> Go Back Home </a></td>";
echo"</tr>";
echo"</table>";
}
}
$con->query($sql3);
echo("<p>Successfully Retrieved The Results For The Quizzes Students Have Taken.</p>");
echo("<p><a href='javascript:history.back(1);'>Return To Previous Page</a></p>");
?>
これはコードの結果であり、ユーザーに出力される内容です。
[出力結果] [1] [1]:私はあなたの参加条件に問題がある見るものからhttps://i.stack.imgur.com/hn5jm.png
「
これは、クエリに基づいて見つかった各結果の結果の新しいテーブルをエコーすることになっています。また、他のページのループ内のテーブルと同じコードを持ちますが、この問題は発生しません。 –
通常はSQLの問題のb/cですが、結合によっておそらく余分なデータが返され、コードがそのセットをループしています。あなたはデータベース内のSQLだけを実行して、生の結果を見ることができますか? – dpp