我正在制作一个从数据库中提出问题和答案的表单,我有几个问题,但我想在每个页面上回答一个问题,然后转到下一个问题。我怎么能做到这一点?我需要一些提示。
我带来问题和答案的代码如下:
echo "<form method='post'>";
$sql= "SELECT pid, qid, question_link FROM question ORDER BY qid ASC LIMIT 1";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_array()) {
$pid1= $row['pid'];
$qid1= $row['qid'];
echo $row['question_link']."<br>";
}
}
$sql1= "SELECT pid, qid, aid, answer, points FROM answer_det WHERE pid=$pid1 AND qid=$qid1";
$result1 = $mysqli->query($sql1);
if ($result1->num_rows > 0) {
while($row = $result1->fetch_array()) {
$answer= $row['answer'];
$aid= $row['aid'];
echo "<input type='radio' name='answers' value='".$answer."'/>".$answer."<br>";
}
}
echo "<input type='submit' value='Submit'></form>";
我应该创建另一个将数据保存到数据库中的PHP页面并显示下一个问题吗?或者是否有任何功能可以做到这一点?
答案 0 :(得分:1)
取决于具体情况。如果您希望用户在途中停下来并且可能在下次返回并完成它,那么DB是一个不错的选择。否则你可以用session来存储他们的进度。
<?php
session_start();
if(isset($_POST['name'])){
//store answers in session
$new = (!empty($_SESSION['session_name']))? $_SESSION['session_name'].'|'.$_POST['name'] : $_POST['name'];
//split session into an array
$_SESSION['session_name'] = $new;
}
else if(isset($_POST['clear'])){
if(!empty($_SESSION['session_name'])){
unset($_SESSION['session_name']);
echo "cleared";
}
else
echo "Nothing to Clear";
}
if(!empty($_SESSION['session_name'])){
var_dump($_SESSION['session_name']);
}
//finish the procees here befor storing into database;
//use foreach to itterate btw arrays and match anwers
//$_SESSION['session_name'] = explode('|', $_SESSION['session_name']);
//answer table $answers = array('a','b','c','d','e');
/* foreach($_SESSION['session_name'] as $key => $ans){
if($ans == $answer[$key]){
//right procesesor
}
else{
//wrong procesesor
}
} */
//handle the right and wrong process get to total score the store in db
?>
<form method="post" action="index.php">
<input name="name" type="text" placeholder="">
<input type="submit" value="submit">
</form>
<form method="post" action="index.php">
<input type="submit" name="clear" value="clear">
</form>
简单的基本演示,了解会话如何在不查询数据库的情况下完成工作。