title: save user input data in multiple page php form
body: <p>I have a multiple page form in PHP. I also created a login page in which username and passwords are stored in a session(I used <a href="http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL" rel="nofollow">this tutorial</a>). Now, I am a bit confused about these things:</p>
  
  <ol>
  <li><p>I would like to enable user to fill the form from the last saved session (e.g., in case of computer or browser crash, failed internet connection, etc., he will not rewrite all the data he previously inserted).</p></li>
  <li><p>Also, I am not sure about how to store data inserted by each user in Mysql table. Is it OK if I just insert all users answers into one table (Let's say "Answers") with these fields: Unique_Id, UserId, question 1, question 2, ... )? Or, it might be better to insert data from each table into a separate table with the same design, for example, table 1: (userId, q1, q2,.., q5), table 2: (userId, q6,q7,..q10) and so on.  </p></li>
  </ol>
  
  <p>This is the code for one of the form pages (Page2, other pages are similar)</p>
  
  <pre><code>&lt;?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 =&gt; $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.
    }
   }
  ?&gt; 
  
  &lt;html&gt;
  &lt;head&gt;
  &lt;link type="text/css" rel="stylesheet" href="style.css" media=screen&gt;
  
  &lt;style&gt;
  .error {
   color: #FF0000;
  
   }
  &lt;/style&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
   &lt;div id="show"&gt;   
  
  &lt;span id="error"&gt;
  &lt;!---Initializing Session for errors--&gt;
  &lt;?php
  if (!empty($_SESSION['error_page2'])) {
   echo $_SESSION['error_page2'];
   unset($_SESSION['error_page2']);
   }
   ?&gt;
   &lt;/span&gt;
  
   &lt;form id="form2" action="page3.php" method="post"&gt;
  
  &lt;div class="meter"&gt;&lt;span style="width: 40%"&gt;Step 2&lt;/span&gt;&lt;/div&gt;
  &lt;fieldset id = "Form_Questions"&gt; 
  &lt;fieldset id = "q8"&gt; &lt;legend class="Q8"&gt;&lt;/legend&gt;
  &lt;label class="question"&gt; What are your favourite genres of movies?&lt;span&gt;*&lt;/span&gt;&lt;/label&gt;
  &lt;div class="fieldset content"&gt; 
  
  &lt;style type="text/css"&gt;
  .checkbox-grid li {
  display: block;
  float: left;
  width: 25%;
  margin-bottom:5px; 
  }     
  &lt;/style&gt;
  
  &lt;title&gt;Survey Form&lt;/title&gt;
   &lt;meta http-equiv="content-Type" content="text/html: charset=UTF-8" /
   &lt;ul class="checkbox-grid"&gt;
  &lt;li&gt;&lt;input type="checkbox" name="q8[]" value="Action"&gt;&lt;label for="checkgenre[]"&gt;Action&lt;/label&gt;&lt;span&gt;&lt;/span&gt;&lt;/li&gt;
  &lt;li&gt;&lt;input type="checkbox" name="q8[]" value="Adventure"&gt;&lt;label for="checkgenre[]"&gt;Adventure&lt;/label&gt;&lt;/li&gt;
  &lt;li&gt;&lt;input type="checkbox" name="q8[]" value="Comedy"&gt;&lt;label for="checkgenre[]"&gt;Comedy&lt;/label&gt;&lt;/li&gt;
  &lt;li&gt;&lt;input type="checkbox" name="q8[]" value="Animation"&gt;&lt;label for="checkgenre[]"&gt;Animation&lt;/label&gt;&lt;/li&gt;
  &lt;li&gt;&lt;input type="checkbox" name="q8[]" value="Drama"&gt;&lt;label for="checkgenre[]"&gt;Drama&lt;/label&gt;&lt;/li&gt; 
  
   &lt;/ul&gt;
  &lt;/div&gt;
  &lt;/fieldset&gt;
   //REST OF QUESTIONS
    .....
     ....
  &lt;input class="mainForm" type="submit" name="continue" value="Save and Continue" /&gt;  
  
  &lt;/form&gt;
  </code></pre>