2015-12-29 60 views
5

我正在製作從數據庫中提出問題和答案的表單,我有幾個問題,但我想在每個頁面上回答一個問題,然後進入下一個問題。我怎樣才能做到這一點?我需要一些提示。如何使用該表格挑選問題並將數據庫中的答案保存到數據庫中

我對帶來的問題和答案的代碼如下所示:

echo "<form method='post'>"; 
 
\t $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()) { 
 
\t $pid1= $row['pid']; 
 
\t $qid1= $row['qid']; 
 
\t 
 
\t echo $row['question_link']."<br>"; 
 
\t } 
 
    
 
} 
 

 
\t $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()) { 
 
\t \t $answer= $row['answer']; 
 
\t \t $aid= $row['aid']; 
 
\t echo "<input type='radio' name='answers' value='".$answer."'/>".$answer."<br>"; 
 
\t } 
 
    
 
} 
 

 
echo "<input type='submit' value='Submit'></form>";

我應該做的是將數據保存到數據庫中,並顯示下一個問題的另一個PHP頁面?或者有什麼功能可以做到這一點?

回答

1

取決於案件。如果你希望用戶在途中停下來,下次回來完成它,那麼DB是一個不錯的選擇。否則你可以使用會話來存儲他們的進度。

<?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> 

簡單基本演示了會話如何在不查詢db的情況下完成工作。

+0

我不希望用戶回來。他們只會做一次測試,不能回到前面的問題 –

+0

,然後使用會話來存儲他們的進度。 –

+0

你是怎麼使用session的?我不擅長會議,但是我應該在會話中保存所有內容,然後在測試結束時將其保存在數據庫中? –

相關問題