【问题标题】:how to send database records one by one to another page in php如何在php中将数据库记录一个接一个地发送到另一个页面
【发布时间】:2017-12-20 10:06:27
【问题描述】:

我正在创建一个类似http://www.sqlquiz.com/ 的测验系统,但结果页面有问题。 我已经创建了 4 个页面

index.php ---->welcomePage 我在查询字符串中使用了变量 n

index.php

<p><a href="quizmain.php?n=1">Start SQL Quiz</a></p>

2.quizmain.php------------>此页面显示问题及其选项和之后 获得每个问题的响应,它将转到计算分数的 process.php 页面,并且每次计数器增加 1,以便在达到第 10 个问题后显示最终结果页面

quizmain.php

 <?php 
    session_start();
    require_once("connection.php");
    extract($_REQUEST);

    $number = (int) $_GET['n']; //starting value 1
    echo $number;
    $n1=rand(1,100);

    $_SESSION['RQuestionNumber']=$n1;

    $q=mysql_query("select * from quiz WHERE qno = '".$n1."'");

    $a=mysql_fetch_array($q);


    echo $a['qno'];
    echo $a['ans'];

    ?>




<body>
    <table>
    <tr>
      <td width="757" height="390"><div align="center">


            <form method="post" action="process1.php">

               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type="radio" name="question" value="a" />
              <?php echo $a[2]?></label>
          </p>
        <p><br />
              <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type="radio" name="question" value="b" />
                <?php echo $a[3]?></label>
          </p>
        <p><br />
              <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type="radio" name="question" value="c" />
                <?php echo $a[4]?></label>
          </p>
        <p><br />
              <label> </label>
              <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type="radio" name="question" value="d" />
                <?php echo $a[5]?></label>
          </p>

        <p>&nbsp; </p>
        <p>
            <label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <input type="submit" value="Submit" name="Submit" />
          <input type="hidden" name="number" value="<?php echo $number; ?>" />
          </form>
            </label>
            <br />
            <br />
            <br />
        </p></td>
    </tr>
  </table>
</body>
</html>

process.php

 <?php 
    session_start(); 
    require_once('connection.php');
    extract($_REQUEST);
        //Check to see if score is set_error_handler
        if(!isset($_SESSION['score'])){
            $_SESSION['score'] = 0;
        }

        if($_POST)
        {
            $number = $_REQUEST['number'];  //value of number is 1 initially 
            $selected_choice = $_REQUEST['question'];
            $next = $number+1; //

        $total=10;  
        $_SESSION['RQuestionNumber'];


        $q = mysql_query("SELECT ans FROM quiz WHERE qno = '".$_SESSION['RQuestionNumber']."'");
        $result=mysql_fetch_array($q);

        //$store=array();



            //Compare
            if($result[0][0] == $selected_choice){
                //Answer is correct
                $_SESSION['score']++;
            }


            //Check if last question
            if($number == $total){
                header("location: resultTable.php");
                exit();
            } else {
                header("location: quizmain.php?n=".$next); //now the value of n is 2 
            }
        }
        ?>

resultTable.php

现在在此页面中,我想打印测验期间出现的所有问题(同一组问题)以及标记的答案和正确的答案 我尝试过使用会话变量,但它不起作用。

<?php session_start(); ?>
            <p>Final Score: <?php echo $_SESSION['score']; ?></p>

<?php session_destroy(); ?>

【问题讨论】:

  • process.php 中的行 $_SESSION['RQuestionNumber']; 看起来不对 - 没有设置值 - 这不应该是 $_SESSION['RQuestionNumber']=$number; 吗??

标签: php mysql database session


【解决方案1】:

你的会话值每次都会被覆盖,所以最后你会得到最后一个问题 ID。如果您想要所有问题 ID,请使用数组。试试这个:

quizmain.php

<?php 
    session_start();
    require_once("connection.php");
    extract($_REQUEST);

    $number = (int) $_GET['n']; //starting value 1
    echo $number;
    $n1=rand(1,100);

    $_SESSION['RQuestionNumber'][]=$n1;

    $q=mysql_query("select * from quiz WHERE qno = '".$n1."'");

    $a=mysql_fetch_array($q);


    echo $a['qno'];
    echo $a['ans'];

    ?>




<body>
    <table>
    <tr>
      <td width="757" height="390"><div align="center">


            <form method="post" action="process1.php">

               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type="radio" name="question" value="a" />
              <?php echo $a[2]?></label>
          </p>
        <p><br />
              <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type="radio" name="question" value="b" />
                <?php echo $a[3]?></label>
          </p>
        <p><br />
              <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type="radio" name="question" value="c" />
                <?php echo $a[4]?></label>
          </p>
        <p><br />
              <label> </label>
              <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type="radio" name="question" value="d" />
                <?php echo $a[5]?></label>
          </p>

        <p>&nbsp; </p>
        <p>
            <label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <input type="submit" value="Submit" name="Submit" />
          <input type="hidden" name="number" value="<?php echo $number; ?>" />
          </form>
            </label>
            <br />
            <br />
            <br />
        </p></td>
    </tr>
  </table>
</body>
</html>

process.php

<?php 
    session_start(); 
    require_once('connection.php');
    extract($_REQUEST);
        //Check to see if score is set_error_handler
        if(!isset($_SESSION['score'])){
            $_SESSION['score'] = 0;
        }

        if($_POST)
        {
            $number = $_REQUEST['number'];  //value of number is 1 initially 
            $selected_choice = $_REQUEST['question'];
            $next = $number+1; //

        $total=10;  
        $last_que = $_SESSION['RQuestionNumber'][count($_SESSION['RQuestionNumber'])-1];
       // $_SESSION['RQuestionNumber'];


        $q = mysql_query("SELECT ans FROM quiz WHERE qno = '".$last_que."'");
        $result=mysql_fetch_array($q);

        //$store=array();



            //Compare
            if($result[0][0] == $selected_choice){
                //Answer is correct
                $_SESSION['score']++;
            }


            //Check if last question
            if($number == $total){
                header("location: resultTable.php");
                exit();
            } else {
                header("location: quizmain.php?n=".$next); //now the value of n is 2 
            }
        }
        ?>

resultTable.php

<?php session_start(); ?>
            <p>Final Score: <?php echo $_SESSION['score']; ?></p>
            <?php
            var_dump($_SESSION['RQuestionNumber']); //You will get all question number here
            ?>

<?php session_destroy(); ?>

【讨论】:

    【解决方案2】:

    您可以在会话中存储数组,因此每次运行 process.php 时,您都可以添加一个带有问题编号和给出答案的数组,这样在 resultTable.php 中您就可以从数据库中选择 10 个问题并进行比较给出的答案。

    添加到process.php中:

    if(!isset($_SESSION['answers'])) {
      $answers = new array();
    } else {
      $answers = $_SESSION['answers'];
    }
    
    $answers[] = array($_SESSION['RQuestionNumber'], $selected_choice);
    
    $_SESSION['answers'] = $answers;
    

    所以这段代码的作用是创建一个数组来存储问题和用户给出的答案并将其保存在会话中,这样您就可以在 resultTable 上循环该数组并在测验中显示所有问题。

    PS:这是非常基本的代码,你需要做很多事情才能在你的问答游戏中使用它,即你应该检查数组是否没有在之前的运行中设置

    【讨论】:

    • 先生,我得到了问题的数字和用户给出的所有问题的答案,但是我如何使用数据库中的这些信息获取问题及其正确选择
    【解决方案3】:

    您可以为此使用会话
    会话的简单语法是:
    session_start(); $_SESSION['score'] = $data; 现在您可以通过像这样开始会话在另一个页面上使用您的 $data
    session_start(); echo $_SESSION['score'];

    要删除数据,您应该销毁会话

    session_destroy();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 2013-01-24
      • 2021-01-30
      • 2019-03-12
      • 2011-12-05
      • 2014-05-15
      相关资源
      最近更新 更多