【发布时间】:2014-10-03 10:21:58
【问题描述】:
我在 PHP 中有一个多页表单。我还创建了一个登录页面,其中用户名和密码存储在会话中(我使用了this tutorial)。现在,我对这些事情有点困惑:
我想让用户从上次保存的会话中填写表格(例如,在计算机或浏览器崩溃、互联网连接失败等情况下,他不会重写他之前插入的所有数据) .
另外,我不确定如何将每个用户插入的数据存储在 Mysql 表中。如果我只是将所有用户的答案插入到一个包含以下字段的表中(比如说“答案”)是否可以:Unique_Id、UserId、问题 1、问题 2、...)?或者,最好将每个表中的数据插入到具有相同设计的单独表中,例如表 1:(userId, q1, q2,.., q5),表 2:(userId, q6,q7, ..q10) 等等。
这是其中一个表单页面的代码(Page2,其他页面类似)
<?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>
【问题讨论】: