【问题标题】:PHP/MySQL "Quiz" Game - Form Input ValuePHP/MySQL“问答”游戏——表单输入值
【发布时间】:2013-12-30 20:46:10
【问题描述】:

好的,所以我已经为此工作了两天 - 我的代码有些草率和混乱,但我已经处理了数百个问题、网站等等等。寻找答案或简单的解释我明白;不幸的是,我的尝试仍然没有成功。

我正在用 PHP/HTML 构建一个“测验”游戏 - 该网站引用了一个数据库,特别是一个标有“答案”的表格,其中包含以下信息:

 - ID: Auto-Increment
 - Question: Varchar
 - Answer: Varchar
 - Comment: Varchar

现在,关于网站的一些信息 - 一旦用户登录,他/她就可以“玩”游戏;游戏只是一个 HTML 表单,上面显示了一个随机的“答案表”问题。该表单有 4 个用户输入,但只需要两个。让我进入代码细节,然后我会问我的问题:

我的 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="ID" value="<?php echo $row["ID"]; ?>" />
           <input type="hidden" name"Answer" 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 页面(表单操作)看起来像这样——我只会给出一个快照,而不是全部:

 <?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'];

          $questionID = $row['ID'];

    if ($questionID == "1" && $player1Answer != "Red"){
        echo $incorrect1Msg;
    }elseif ($questionID == "2" && $player1Answer != "4"){
        echo $incorrect1Msg;
    }else {
        echo $correct1Msg;
    }

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

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

请注意,gameSubmit.php 页面也有相同的消息,以及用于 player3Answer 和 player4Answer 的 if...elseif... 语句。

所以我的问题是......

如果用户登录并打开 index.php 页面,系统会提示他/她“echo $row ["Question"]”(这是使用 $query = "SELECT 从 MySQL 数据库中提取的问题* FROM answers ORDER BY RAND() LIMIT 1"; - 然后用户继续在每个玩家的相应文本输入中输入答案。一旦用户单击提交按钮,表单将重定向到 gameSubmit.php - 一旦加载,if(isset ($_POST['submitted'])){ 启动并交叉检查每个用户的回答并显示相应的消息。

目前,我的表单重定向到 gameSubmit.php,但是,它没有参考上一个问题的正确答案 - 因此,当“评分”答案时出现相同的答案,这真是太幸运了。

为了在表单操作页面上实现输入验证,我需要做什么/需要更正什么?

再一次,我只是想随机检索一个问题,并在提交时检查输入的答案和正确答案 - 我还希望我的代码能够检索正确答案,而不是我必须输入每个答案,这样,如果添加了一条记录,我就不必更新代码了。

感谢您的时间和帮助,非常感谢! (这是决赛周,我的压力不能再大了)

  • 石榴

【问题讨论】:

标签: php html mysql forms validation


【解决方案1】:

只需将 POST 元素从索引页面传递到带有问题 ID 的 gameSubmit.php。

在索引页面中添加一个隐藏元素,如..

<input type="hidden" name="questionId" value="<?php echo $row['id']; ?>">

所以,您可以使用$_POST['questionId'] 在 pageSubmit.php 中获取问题 ID

【讨论】:

  • 这不是我已经拥有的吗?另外,我测试了这种方法,它似乎不起作用,它总是给我一个错误的答案。有什么想法吗?
  • sriraman - 我想通了!事实证明,我在隐藏值名称之后缺少一个“=”符号,因此它没有将答案传递给操作页面。感谢您的帮助!
猜你喜欢
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-08
相关资源
最近更新 更多