Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am creating a website which users can register for membership - once registered, the user can view the index.php page which contains a form:

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");
 }
 ?>

Then on the form action page (gameSubmit.php) I have the following code:

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");
 }
 ?>

So as you can see, I'm attempting a few different methods of verifying the players answer with the last question. Basically, I am trying to make it so that when the user submits the initial form (located on index.php) - the form action (gameSubmit.php) will check each player answer with the correct answer from the question displayed on the last page (i.e. the question the players responded to) - I have tirelessly scoured the forums, websites, books and more for an answer to my problem; I just can't seem to find an explanation I understand or that works for me.

share|improve this question
 
Mike B - I apologize, how do I take the user answers and on form submit check if they inputted the correct answer on the gameSubmit.php page? Sorry, its difficult for me to word. Currently, my method of validation isn't consistent it doesn't cross-reference the question/answer that the players answered - it is completely random. –  rockmandew Dec 12 '13 at 22:21
 
mysql_num_rows is a double error. 1. never use mysql_num_rows unless you are trying to get the actual row count. Use _fetch_row directly and test for null. 2. Don't use mysql_ functions. –  cwallenpoole Dec 12 '13 at 22:21
 
Also, there is a live preview of the website at kethcart.uwmsois.com/qm/htdocs/Home/index.php - you will have to register an account but you will find the game form on the homepage (index.php) feel free to test it out. –  rockmandew Dec 12 '13 at 22:22
 
input type="hidden" name"qAnswer" value="<?php echo $row["Answer"]; ?>" You know this isn't hidden from users right? ie in very quick and simple ways they can get the answer. –  James Dec 12 '13 at 22:25
 
cwallenpoole - I understand this isn't the correct format, I realized that when I was searching for an answer; I also realize the ORDER BY RAND() method is technically a poor option, however, this is a school assignment and it must be completed in this manner - or as close as possible to it. –  rockmandew Dec 12 '13 at 22:26
show 3 more comments

1 Answer

up vote 0 down vote accepted

Your action page is grabbing a new random question.

You need to do something like:

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

I would recommend:

  • use mysqli_ functions
  • avoid SELECT * - instead, specify the columns you require
  • escape user inputs before putting them in a MySQL query, such as mysqli_escape_string
share|improve this answer
 
rybo111 - Thank you very much for the answer, I am applying the changes now and will let you know how it works. –  rockmandew Dec 13 '13 at 1:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.