我知道問題,但我似乎無法修復它,我希望有人在這裏可以引導我在正確的方向,我想要做的是檢查看看如果用戶在對照答案數據庫進行檢查並將其插入數據庫之前已經提交了正確的答案,只需要停止多次回答相同的問題,我是MYSQLi的新手而且不擅長,仍然學習它。PHP檢查表單是否已經使用num行提交
我目前有至今是:
$mysqli = new mysqli($host,$username,$password,$database);
if($mysqli -> connect_error)die($mysqli->connect_error);
$questionID = $_POST['id'];
$userAnswer = $_POST['answer'];
$userAnswer = strtolower(trim($userAnswer));
$questionValue = $_POST['qValue'];
$teamName = $_SESSION['user_email'];
$user_id = "SELECT t.teamID,t.questionGroupID FROM team as t WHERE t.teamName ='$teamName'";
$result2 = $mysqli->query($user_id);
if ($result2->num_rows > 0) {
// output data of each row
while($row = $result2->fetch_assoc()) {
$userID = $row["teamID"];
}
}
$query = "SELECT answers FROM answers WHERE questionID=?";
$statement = $mysqli->prepare($query);
$statement ->bind_param('i', $questionID);
$statement->execute();
$statement->bind_result($answer);
//checking the database to see if the current question is there from the current user/teamName
if ($result = mysqli_query($mysqli, "SELECT * FROM submissions where teamID='$teamName' and questionID='$questionID'")) {
/* determine number of rows result set */
$row_cnt = mysqli_num_rows($result);
/* close result set */
mysqli_free_result($result);
}
/* close connection */
mysqli_close($mysqli);
//checking to see if it returns a result
if(($row_cnt)= 0){
while ($statement->fetch()) {
if ($answer != $userAnswer) {
echo '<br><br><div class="alert alert-danger"><h5>
<strong>Sorry!</strong> the answer is incorrect! Please Try again!.</h5>
</div>';
"<h3>Sorry the answer is incorrect! Please Try again!</h3><br>";
//return to previous Page
echo '<a href="./question.php?id=' . $questionID . '" class="btn btn-primary btn-block">Return to Question </a>';
$statement->free_result();
$sql = "INSERT INTO `submissions`(`submissionsID`, `teamID`, `questionID`, `answer`,`qValue`,`status`,`timestamp`) VALUES (null,'$teamName','$questionID','$userAnswer','0','Wrong',NOW())";
if (mysqli_query($mysqli, $sql)) {
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($mysqli);
}
} else {
echo '<br><br><div class="alert alert-success"><h5>
<strong>Success!</strong> Correct Answer, Good Luck with the Next </h5>
</div>';
echo "<a href='questionList.php' class='btn btn-success btn-block'>Continue with other questions! </a>";
$statement->free_result();
//MySqli Insert Query
$sql = "INSERT INTO `submissions`(`submissionsID`, `teamID`, `questionID`, `answer`,`qValue`,`status`,`timestamp`) VALUES (null,'$teamName','$questionID','$userAnswer','$questionValue','Correct',NOW())";
if (mysqli_query($mysqli, $sql)) {
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($mysqli);
}
}
}
}else{
echo '<br><br><div class="alert alert-warning"><h5>
<strong>Already Answered!</strong> Good Luck with the Next </h5>
</div>';
echo "<a href='questionList.php' class='btn btn-warning btn-block'>Continue with other questions! </a>";
}
我已經測試過它很多方面,我需要做的就是運行一個檢查,看看如果登錄用戶已經正確地回答了questionID目前,我正在使用num_rows來查看它是否大於0,如果它大於0,他們已經回答了它。
所以我的問題是,我正確接近它,我應該採取什麼方法?
您的邏輯是回到前面。你不需要先檢查。而應在用戶和答案上建立一個組合鍵。 – Strawberry
另請參閱如何準備第二個查詢?那很好。 – Strawberry