在php函数中递增会话变量以及将其传递给另一个页面

时间:2014-02-02 15:01:03

标签: php

我目前正在创建一个数据库驱动的多项选择测验但是我遇到了一些问题,我发现我的得分会话变量没有正确递增而且未正确传递到下一页,如下所示

欢迎任何建议

主要测验页面

<?php session_start(); ?>
<html>
<head>
<title> World Cup Quiz  </title>
<style>
.main{margin-top:2cm};
.main{margin-left:10cm};
.main{margin-bottom:2cm};
.main{margin-right:10cm};

</style>
</head>
<body>
<div align = center >
<br />

<div class="main"  align =center>

<strong> World Cup Quiz</strong></div>
<?php


function Score($userScore)
{
    if(isset($_POST['correctAnswer'] ))

    {
        $_SESSION['score']=$userScore+1;

    }

    else
    {
     $_SESSION['score']=$_SESSION['score'];

     }




}


include ("dbConnect.php");

if (!isset($_SESSION['number']))
{
    $_SESSION['number']=1;
}

if (!isset($_SESSION['score']))

{
    $_SESSION['score']=0;

}


$questionNumber = $_SESSION['number'];
$userScore=$_SESSION['score'];
$number= rand(1,16);

//search database for generated number and match ID
$dbQuery= "SELECT * FROM `questions 1.0` WHERE  `ID` =$number";
$dbResult=mysql_query($dbQuery);

echo "Question:".$questionNumber."/5<br>";

//Assign variables to each attribute

while ($dbRow=mysql_fetch_array($dbResult))

{
 if ($_SESSION['number']>5)

    {
        header("Location: results.php");
        $_SESSION['number']=1;
        break;


    }

   $theID=$dbRow["ID"];
   $theQuestion=$dbRow["Question"];
   $theAnswer1=$dbRow["Correct Answer"];
   $theAnswer2=$dbRow["Wrong Answer 1"];
   $theAnswer3=$dbRow["Wrong Answer 2"];
   $theAnswer4=$dbRow["Wrong Answer 3"];
   $_SESSION['number']=$questionNumber+1;


}

  //Print Questions and Answers


    echo '<strong>'."$theQuestion".'</strong><br>';

   ?> <form name="correctAnswer" form method="post" action="quiz.php" onSubmit="Score(<?$userScore?>)"> 
   <table border="0">
                <tbody>
                    <tr>
                    <td>
                          <?php
                           echo "$theAnswer1";?></td><td> <input type="radio" id="correct_answer" name="correctAnswer"></td>
                    </tr>
                       <tr>
                       <td>
                      <?php
                       echo "$theAnswer2"; ?></td><td> <input type="radio" id="wrong_answer1" name="wrongAnswer1"> </td>
                       </tr>
                       <tr>
                       <td>
                       <?php
                       echo "$theAnswer3"; ?> </td><td><input type="radio" id="wrong_answer2" name="wrongAnswer2"> </td>
                       </tr>
                       <tr>
                       <td>
                       <?php
                       echo "$theAnswer4"; ?></td><td><input type="radio" id="wrong_answer3" name="wrongAnswer3"> </td>
                       </tr>
                       <input type="hidden" name="score" value="userScore">
                       <tr>
                       <td>
                       <input type="submit" value="Submit Answer"></td>
                       </tr>
             </tbody>
        </table>

   </form>


</div>
</body>

结果页面

<html>

<head>

<title> Result</title>
<style>
.main{margin-top:2cm};
.main{margin-left:10cm};
.main{margin-bottom:2cm};
.main{margin-right:10cm};

</style>

</head>
<body>
    <div align =center class="main">
    <?php

    $score=$_SESSION['score'];

    echo "Congratulations you scored $score /5<br>"

    ?>

    <form action="menu.php">

    <input type="submit" value="Return to Main Menu">
    </form>
    </div>
</body>

</html>

希望你能帮忙

1 个答案:

答案 0 :(得分:0)

您没有运行Score()功能。 在include行下添加。

include ("dbConnect.php");
if( isset( $_POST[ 'submit' ] )
{
    Score( $_POST[ 'userScore' ] );
}