PHP / MySQL表单输入 - 测验游戏

时间:2013-12-12 22:16:36

标签: php mysql forms validation

我正在创建一个用户可以注册会员资格的网站 - 一旦注册,用户就可以查看包含表格的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)将使用最后一页显示的问题中的正确答案检查每个玩家的答案(即球员回答的问题) - 我不知疲倦地搜索论坛,网站,书籍等,以回答我的问题;我似乎无法找到我理解或对我有用的解释。

1 个答案:

答案 0 :(得分:0)

您的操作页面正在抓取一个新的随机问题。

您需要执行以下操作:

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

我建议:

  • 使用mysqli_函数
  • 避免SELECT * - 而是指定您需要的列
  • 在将用户输入放入MySQL查询之前对其进行转义,例如mysqli_escape_string