如何在php中将数据库记录逐个发送到另一个页面
我正在创建一个类似的测验系统,结果页面中有问题。 我已经创建了4页 index.php-->welcomePage,其中我在查询字符串中使用变量n index.php如何在php中将数据库记录逐个发送到另一个页面,php,mysql,database,session,Php,Mysql,Database,Session,我正在创建一个类似的测验系统,结果页面中有问题。 我已经创建了4页 index.php-->welcomePage,其中我在查询字符串中使用变量n index.php <p><a href="quizmain.php?n=1">Start SQL Quiz</a></p> <?php session_start(); require_once('connection.php'); extract($_REQUE
<p><a href="quizmain.php?n=1">Start SQL Quiz</a></p>
<?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
}
}
?>
<?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">
<input type="radio" name="question" value="a" />
<?php echo $a[2]?></label>
</p>
<p><br />
<label>
<input type="radio" name="question" value="b" />
<?php echo $a[3]?></label>
</p>
<p><br />
<label>
<input type="radio" name="question" value="c" />
<?php echo $a[4]?></label>
</p>
<p><br />
<label> </label>
<label>
<input type="radio" name="question" value="d" />
<?php echo $a[5]?></label>
</p>
<p> </p>
<p>
<label>
<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>
<?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
}
}
?>
<?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.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">
<input type="radio" name="question" value="a" />
<?php echo $a[2]?></label>
</p>
<p><br />
<label>
<input type="radio" name="question" value="b" />
<?php echo $a[3]?></label>
</p>
<p><br />
<label>
<input type="radio" name="question" value="c" />
<?php echo $a[4]?></label>
</p>
<p><br />
<label> </label>
<label>
<input type="radio" name="question" value="d" />
<?php echo $a[5]?></label>
</p>
<p> </p>
<p>
<label>
<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>
每次您的会话值都将被覆盖,所以最后您将获得最后一个任务id。如果您需要所有问题id,请使用数组。试试这个:
quizmain.php
<p><a href="quizmain.php?n=1">Start SQL Quiz</a></p>
<?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
}
}
?>
<?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">
<input type="radio" name="question" value="a" />
<?php echo $a[2]?></label>
</p>
<p><br />
<label>
<input type="radio" name="question" value="b" />
<?php echo $a[3]?></label>
</p>
<p><br />
<label>
<input type="radio" name="question" value="c" />
<?php echo $a[4]?></label>
</p>
<p><br />
<label> </label>
<label>
<input type="radio" name="question" value="d" />
<?php echo $a[5]?></label>
</p>
<p> </p>
<p>
<label>
<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>
<?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
}
}
?>
<?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(); ?>
您可以在会话上存储数组,因此每次运行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;
因此,这段代码所做的是创建一个数组来存储问题和用户给定的答案,并将其保存在会话中,这样您就可以在结果表上循环此数组,并在测验中显示所有问题
PS:这是非常基本的代码,要在问答游戏中使用它,您需要做很多事情,即,您应该检查数组是否不是从以前的运行中设置的您可以使用会话进行设置
会话的简单语法是:
session_start();
$\会话['score']=$数据;
现在,只需像这样启动会话,您就可以在另一个页面上使用$data了
session_start();
echo$_会话['score']代码>
要删除数据,应销毁会话
session_destroy();
行$\u会话['RQuestionNumber']process.php
中的code>看起来错误-没有设置值-应该不是$\u SESSION['RQuestionNumber']=$number代码>?先生,我可以获得所有问题的问题编号和用户给出的答案,但如何使用数据库中的这些信息获取问题及其正确选择