2015-10-21 98 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>