2015-10-21 96 views
1

我知道问题,但我似乎无法修复它,我希望有人在这里可以引导我在正确的方向,我想要做的是检查看看如果用户在对照答案数据库进行检查并将其插入数据库之前已经提交了正确的答案,只需要停止多次回答相同的问题,我是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,他们已经回答了它。

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

+0

您的逻辑是回到前面。你不需要先检查。而应在用户和答案上建立一个组合键。 – Strawberry

+0

另请参阅如何准备第二个查询?那很好。 – Strawberry

回答

1

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

$row_cnt = $result->num_rows; 

而不是

$row_cnt = mysqli_numrows($result); 

也不要忘了,$row_cnt将等于-1任何形式的查询错误的情况下,所以你应该检查该假设所有阿尔恩前值't 0是有效的。

+0

谢谢你的回答,我已经尝试过了,并将计数器设置为<= 0,但现在我的问题是它似乎在拖延,当我设置计数器= 0时,我得到声明的结尾,它说你已经回答了这个问题),当我把它设置为其他任何东西时,网页显示为空 – Keiththolt

+0

如果$ row_cnt =正确的语法是==,我还需要比较运算符不是赋值操作符。 – PC3TJ

+0

尝试设置error_reporting(E_ALL);在代码的开头,看看你是否得到任何语法错误 – PC3TJ

0

我建议你看看自然语言处理(NLP)技术。如果你的答案是单向(一个字)。这种方法没问题。如果你正在处理大小超过1的n-gram,即长句或段落,那么你的方法将无法正常工作。答案可以用不同的方式写。所以我会建议一些语义方法,如LSA(潜在语义分析)或简单的矢量表示模型

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

+0

托马斯,我相信她的问题只是想知道如何检查这个用户是否提交了一个问题的答案,而不是如何检查给出的答案是否正确。简单看看她的代码,看起来可能是多种选择。 – PC3TJ

+0

您好,托马斯,感谢您花时间做出回应,我已经做了比较,如果用户的答案等于数据库中的答案,我目前的问题是我想看看当前用户是否有提交在提交表中等于用户ID和问题ID,如果他们有,这意味着他们已经回答了这个问题,它不能再回答。 – Keiththolt

0

我采取了一种不同的方法来试图使其工作,并最终得到它的工作,只是想发布我的解决方案,并感谢大家的帮助。

<?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: %s\n", 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>