gpt4 book ai didi

PHP 检查表单是否已经使用 num rows 提交

转载 作者:行者123 更新时间:2023-11-28 23:47:53 24 4
gpt4 key购买 nike

我知道这个问题,但我似乎无法解决它,我希望这里的人可以引导我朝着正确的方向前进,我想做的是检查用户是否已经提交了正确的答案在根据答案数据库检查它并将其插入数据库之前提出一个问题,只是为了停止多次回答同一个问题,我是 MYSQLi 的新手,并不擅长它,仍在学习它。

我目前拥有的是:

$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,他们已经回答了。

所以我的问题是,我的做法是否正确,我应该采取什么方法?

最佳答案

这是一个很好的方法。尝试使用

$row_cnt = $result->num_rows;

而不是

$row_cnt = mysqli_numrows($result);

也不要忘记 $row_cnt 在出现任何形式的查询错误时将等于 -1,因此您应该在假设所有非 0 的值都有效之前检查它。

关于PHP 检查表单是否已经使用 num rows 提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33249487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com