PHP 检查表单是否已使用行数提交



我知道这个问题,但我似乎无法解决它,我希望这里有人可以引导我朝着正确的方向前进,我想做的是检查用户是否已经提交了问题的正确答案,然后再根据答案数据库进行检查并将其插入数据库, 只是为了阻止多次回答相同的问题,我是 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>";
}

我已经以大多数方式对其进行了测试,我需要做的是运行检查以查看当前登录的用户是否已经正确回答了问题ID,我正在使用num_rows来查看它是否大于0,如果大于0,他们已经回答了它。

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

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

$row_cnt = $result->num_rows;

而不是

$row_cnt = mysqli_numrows($result);

另外不要忘记,如果发生任何形式的查询错误,$row_cnt将等于 -1,因此在假设所有非 0 的值都有效之前,您应该检查这一点。

我建议你看看自然语言处理(NLP(技术。如果你的答案是单元语法(一个词(。这种方法是可以的。如果您正在处理大小超过 1,即长句子或段落的 n 元语法,那么您的方法将无法很好地工作。答案可以用不同的方式编写。所以我建议一些语义方法,如LSA(潜在语义分析(或简单的向量表示模型

我想不出任何其他方法来解决这个问题。尝试 NLP 方法。会给你真棒的结果。

我采取了不同的方法来尝试让它工作并最终让它工作,只是想发布我的解决方案并感谢大家的帮助。

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Bootstrap Core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom CSS -->
    <link href="css/modern-business.css" rel="stylesheet">
    <!-- Custom Fonts -->
    <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
    <!-- Navigation -->
    <?php include_once('navigation.php');
// establishing the MySQLi connection
require_once('connection-test.php');
$mysqli = new mysqli($host,$username,$password,$database);
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %sn", mysqli_connect_error());
    exit();
}
$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);
$statement->store_result();

?>
    <div class="container">
    <!-- Page Content -->
        <hr>
<?php 
 if ($result4 = $mysqli->query("SELECT * FROM submissions where teamID='$teamName' and questionID='$questionID'"))
                        {
                                // display records if there are records to display
                                if ($result4->num_rows > 0)
                                {
                        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>";
                                }
                                // if there are no records in the database, display an alert message
                                else
                                {
                                        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);
                                }

                            }
                    } 
                                }
                        }
                        // show an error if there is an issue with the database query
                        else
                        {
                                echo "<strong>Error:</strong>" . $mysqli->error;
                        }

?>
        <?php include_once('footer.php'); ?>
    </div>
    <!-- /.container -->
    <!-- jQuery -->
    <script src="js/jquery.js"></script>
    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>
</body>
</html>

相关内容

  • 没有找到相关文章

最新更新