簡體   English   中英

PHP / MySQL表單輸入-測驗游戲

[英]PHP/MySQL Form Input - Quiz Game

我正在創建一個網站,用戶可以注冊成為會員-注冊后,用戶可以查看包含以下表格的index.php頁面:

Index.php

 <?php # index.php
 session_start();
 //check session first
 if (!isset($_SESSION['email'])){
 include ('../includes/header.php');
 }else
     {
 session_start();
 include ('../includes/header.php');
 require_once ('../../mysql_connect.php');
          $query = "SELECT * FROM answers ORDER BY RAND() LIMIT 1"; 
          $result = @mysql_query ($query);
          $num = mysql_num_rows($result);
          if ($num > 0) { // If it ran OK, display all the records.
              while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {      
 ?>

 <div class="newGame">
       <h2>Are you a Question Master?<hr /></h2>
       <h3 style="color:#000">Find Out Now!</h3>
 </div>
 <br />

 <div class="newGameContain">
       <form action="gameSubmit.php" method="post" autocomplete="off">
       <h2><? echo $row["Question"]."<hr />"; ?></h2>
       <h3>Enter Player Answers</h3>
              <p><input type="text" placeholder="Player 1" name="player1" value="<? echo $_POST['player1']; ?>" /> <input type="text" placeholder="Player 2" name="player2" value="<? echo $_POST['player2']; ?>" /></p>
              <p><input type="text" placeholder="Player 3" name="player3" value="<? echo $_POST['player3']; ?>" /> <input type="text" placeholder="Player 4" name="player4" value="<? echo $_POST['player4']; ?>" /></p>
              <p><input type="submit" class="submitButton" /> <input type="reset" class="resetButton" value="Reset" /> </p>
              <input type="hidden" name="questionId" value="<?php echo $row['ID']; ?>" />
              <input type="hidden" name"qAnswer" value="<?php echo $row["Answer"]; ?>" />
              <input type="hidden" name="submitted" value="TRUE" />
 </form>
 <p></p>
 </div>
 <br />


 <?php
                   } //end while statement
          } //end if statement
     mysql_close();
     //include the footer
     include ("../includes/footer.php");
 }
 ?>

然后在表單操作頁面(gameSubmit.php)上,我有以下代碼:

gameSubmit.php

 <?php # index.php
 session_start();
 //check session first
 if (!isset($_SESSION['email'])){
 include ('../includes/header.php');
 }else
     {
 session_start();
 include ('../includes/header.php');
 require_once ('../../mysql_connect.php');
          $query = "SELECT * FROM answers ORDER BY RAND() LIMIT 1"; 
          $result = @mysql_query ($query);
          $num = mysql_num_rows($result);
               if ($num > 0) { // If it ran OK, display all the records.
                  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {      
 ?>

 <? if (isset($_POST['submitted'])){
      $correct1Msg = "<div class='correct1Msg'><p style='color:#000;font-family:Arial, Helvetica, sans-serif;'>Player 1 entered the <span id='answerUnder'>correct answer</span>.</p></div><p></p>";
      $correct2Msg = "<div class='correct2Msg'><p style='color:#000;font-family:Arial, Helvetica, sans-serif;'>Player 2 entered the <span id='answerUnder'>correct answer</span>.</p></div><p></p>";
      $incorrect1Msg = "<div class='incorrect1Msg'><p style='color:#F00;font-family:Arial, Helvetica, sans-serif;'>Player 1 entered the <span id='answerUnder'>incorrect answer</span>.</p></div><p></p>";
      $incorrect2Msg = "<div class='incorrect2Msg'><p style='color:#F00;font-family:Arial, Helvetica, sans-serif;'>Player 2 entered the <span id='answerUnder'>incorrect answer</span>.</p></div><p></p>";

 $player1Answer = $_POST['player1'];
 $player2Answer = $_POST['player2'];
 $player3Answer = $_POST['player3'];
 $player4Answer = $_POST['player4'];

 $correctAnswer = $row['Answer'];
 $questionAnswer = $_POST['qAnswer'];
 $questionID = $row['ID'];
 $answeredID = $_POST['questionId'];
 $id1 = 1;
 $id2 = 2;
 $answer1 = "Red";
 $answer2 = "4";

         if ($player1Answer == $questionAnswer){
             echo $correct1Msg;
         }

         if ($player1Answer != $questionAnswer){
             echo $incorrect1Msg;
         }

         if ($questionID == "1" && $player2Answer == "Red"){
             echo $correct2Msg;
         }elseif ($questionID == "2" && $player2Answer == "4"){
             echo $correct2Msg;
         }else{
             echo $incorrect2Msg;
         }

         if ($questionID == "1" && $player3Answer == "Red"){
             echo $correct3Msg;
         }elseif ($questionID == "2" && $player3Answer == "4"){
             echo $correct3Msg;
         }else{
             echo $incorrect3Msg;
         }

         if ($questionID == "1" && $player4Answer == "Red"){
             echo $correct4Msg;
         }elseif ($questionID == "2" && $player4Answer == "4"){
             echo $correct4Msg;
         }else{
             echo $incorrect4Msg;
         }
 }
 ?>

 <?php
                   } //end while statement
          } //end if statement
     mysql_close();
     //include the footer
     include ("../includes/footer.php");
 }
 ?>

如您所見,我正在嘗試幾種不同的方法來驗證玩家對最后一個問題的回答。 基本上,我試圖做到這一點,以便當用戶提交初始表單(位於index.php上)時,表單操作(gameSubmit.php)將檢查每個玩家的答案,並顯示最后一頁上顯示的問題的正確答案(即玩家回答的問題)-為了解決我的問題,我不知疲倦地搜尋了論壇,網站,書籍等等。 我似乎找不到我理解的解釋或對我有用的解釋。

您的操作頁面正在捕獲一個新的隨機問題。

您需要執行以下操作:

$qid = $_POST["questionId"];
$query = "SELECT * FROM answers WHERE questionId='$qid'";

我建議:

  • 使用mysqli_函數
  • 避免使用SELECT * -相反,請指定所需的列
  • 在將用戶輸入放入MySQL查詢(例如mysqli_escape_string之前先對其進行mysqli_escape_string

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM