以多页php形式保存用户输入数据


save user input data in multiple page php form

我在PHP中有一个多页表单。我还创建了一个登录页面,其中用户名和密码存储在会话中(我使用了本教程)。现在,我对这些事情有点困惑:

  1. 我希望用户能够从上次保存的会话中填写表格(例如,在计算机或浏览器崩溃、互联网连接失败等情况下,他不会重写之前插入的所有数据)。

  2. 此外,我不确定如何将每个用户插入的数据存储在Mysql表中。如果我只将所有用户的答案插入到一个表中(比如"答案"),其中包含以下字段:Unique_Id、UserId、问题1、问题2…),可以吗?或者,最好将每个表中的数据插入到具有相同设计的单独表中,例如,表1:(userId,q1,q2,..,q5)、表2:(userId、q6,q7,..q10)等等

这是其中一个表单页面的代码(第2页,其他页面类似)

<?php
session_start();
 // Checking first page values for empty,If it finds any blank field then redirected to first page.
 if (isset($_POST['submit1'])){
 if (empty($_POST['q1'])
 || empty($_POST['q2'])
 || empty($_POST['q5'])
 || empty($_POST['q6'])){
 // Setting error message
  $_SESSION['error'] = "Mandatory field(s) are missing, Please fill it again";
  header("location: page1.php"); // Redirecting to first page 
  } else {
   foreach ($_POST as $key => $value) {
       if (is_array($_POST[$key])){
            $_SESSION['post'][$key] = implode(',', $_POST[$key]);
         }
       else{
            $_SESSION['post'][$key] = $value;
        }
   }
  }
  } else {
  if (empty($_SESSION['error_page2'])) {
  header("location: page1.php");// Redirecting to first page.
  }
 }
?> 
<html>
<head>
<link type="text/css" rel="stylesheet" href="style.css" media=screen>
<style>
.error {
 color: #FF0000;
 }
</style>
</head>
<body>
 <div id="show">   
<span id="error">
<!---Initializing Session for errors-->
<?php
if (!empty($_SESSION['error_page2'])) {
 echo $_SESSION['error_page2'];
 unset($_SESSION['error_page2']);
 }
 ?>
 </span>
 <form id="form2" action="page3.php" method="post">
<div class="meter"><span style="width: 40%">Step 2</span></div>
<fieldset id = "Form_Questions"> 
<fieldset id = "q8"> <legend class="Q8"></legend>
<label class="question"> What are your favourite genres of movies?<span>*</span></label>
<div class="fieldset content"> 
<style type="text/css">
.checkbox-grid li {
display: block;
float: left;
width: 25%;
margin-bottom:5px; 
}     
</style>
<title>Survey Form</title>
 <meta http-equiv="content-Type" content="text/html: charset=UTF-8" /
 <ul class="checkbox-grid">
<li><input type="checkbox" name="q8[]" value="Action"><label for="checkgenre[]">Action</label><span></span></li>
<li><input type="checkbox" name="q8[]" value="Adventure"><label for="checkgenre[]">Adventure</label></li>
<li><input type="checkbox" name="q8[]" value="Comedy"><label for="checkgenre[]">Comedy</label></li>
<li><input type="checkbox" name="q8[]" value="Animation"><label for="checkgenre[]">Animation</label></li>
<li><input type="checkbox" name="q8[]" value="Drama"><label for="checkgenre[]">Drama</label></li> 
 </ul>
</div>
</fieldset>
 //REST OF QUESTIONS
  .....
   ....
<input class="mainForm" type="submit" name="continue" value="Save and Continue" />  
</form>

如果您想从上次访问中提取问题的答案,那么只需要对数据库进行SELECT查询,以确定他们输入了什么,如果他们以前没有输入答案,则留空。

无法准确理解将所有答案存储在会话中的含义,但如果您想在最后一次性保存所有答案,则最好将答案存储在cookie中。

为了使您的网页动态,您可以将所有问题和答案存储在数据库中。你可以使用这样的布局。。。

Table name: 'Questions'
  ID               int (autonumber)
  Question         text
Table name: 'Answers'
  ID               int (autonumber)
  QuestionID       int
  Answer           varchar(250)
Table name: 'UsersAnswers'
  ID               int (autonumber)
  UserID           int
  AnswerID         int

(这是假设你所有的问题都是多选答案)

这样做将允许您添加任意多的问题,而不必向表中添加额外的列。

编辑

如果你想要动态答案,那么你可以把答案存储在一个表中,比如…

Table name: 'UsersAnswers'
  ID               int (autonumber)
  UserID           int
  QuestionID       int
  Answer           string

此表将允许您存储用户回答的问题的答案。