Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I have a php form containing 5 pages (multiple pages). There are 27 questions and many of them are multiple values. I inserted all the answers into one table "test" and multiple values are inserted in one row, so each row represent answers related to one user. This is the table "test":

enter image description here

Problem:

I would like to have multiple values in separate rows. for example, for q8 I need to have drama and history in different rows and not in one row separated by comma!. However, I don't know how it could be possible since I have 27 questions and 17 of them are multiple values for which user can insert unlimited number of answers (e.g user can choose 10 genres for q8).

This is the html code of one of the pages "page2.php" (since there are 5 pages and I couldn't paste all the codes, I just paste page2 as a sample, other pages are almost the same).

<?php
session_start();    
if (empty ($_SESSION['SESS_USERNAME'])) {
          header ("location:index.php");
          exit;
    }
foreach ($_POST as $key => $value) {
     if (is_array($_POST[$key])){
         $_SESSION['post'][$key] = implode(',', $_POST[$key]);
     }
     else{
         $_SESSION['post'][$key] = $value;
     }
 }
 extract($_SESSION['post']); // Function to extract array.*/
 include('insertPage1.php');
 ?>    
<html>
<head>

 <title>Survey Form</title>
 <meta http-equiv="content-Type" content="text/html: charset=UTF-8" />  
<!-- <script type="text/javascript" src="actors.js"></script>-->
<!--<script type="text/javascript" src="directors.js"></script>-->

 <link type="text/css" rel="stylesheet" href="style.css" media=screen>

<script src="http://thecodeplayer.com/uploads/js/prefixfree-1.0.7.js"type="text/javascript"type="text/javascript"></script>    
</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>-->
<script src="http://thecodeplayer.com/uploads/js/prefixfree-1.0.7.js"type="text/javascript"type="text/javascript"></script>

  <div class="breadcrumb flat">
      <a href="#">Step1</a>
      <a href="#"class="active">Step2</a>
      <a href="#">Step3</a>
      <a href="#">Step4</a>
      <a href="#">Step5</a>
  </div>
<fieldset id = "Form_Questions">   
<fieldset id = "q27"> <legend class="Q27"></legend>
<label class="question"> What are your favorite movies?<span>*</span></label>
<div class="fieldset content"> 
<p>    
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/minified/jquery-ui.min.css" type="text/css" /> 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>    
<div class="movienames">
<a href="#" id="addScnt4">Add more movies</a>    
<div id="m_scents">
<p>
<label style="margin-bottom:10px;" for="m_scnts">
<input class="autofill4" type="text" id="m_scnt" size="20" name="q27[]"
value="" placeholder="Enter text" />
</label>
</p>
</div>    
</div>
</p>
</div>
</fieldset>

<script type="text/javascript">
$(function() {
//autocomplete
  $(".autofill4").autocomplete({
      source: "filmsauto.php",
      minLength: 3
  });                              
});

$(function () {
var scntDiv4 = $('#m_scents');
var l = $('#m_scents p').size() + 1;
$('#addScnt4').on('click', function (q) {
  q.preventDefault();
  q.stopPropagation();
  $('<p><label style="margin-bottom:10px;" for="m_scnts"><input class="autofill4"  type="text" name="m_scnt[]" size="20" id="m_scnt_' + l + '" value=""  placeholder="Add text" /></label  for="remScnt4"> <label style="padding-left:400px;"><a href="#" class="remScnt4">Remove</a></label></p>').appendTo(scntDiv4);

$(function ($) {
  $('#m_scnt_' + l).autocomplete({
    source: "filmsauto.php",
    minLength: 3
 });
});
l++; // should increase counter here
return false;
}); 

$('.movienames').on('click', '.remScnt4', function () {
if (l > 2) {
            $(this).parents('p').remove();
            l--;
        }
        return false;
    });
}); 

</script> 

<fieldset id = "q8"> <legend class="Q8"></legend>
<label class="question"> What are your favourite genres of movies?<span>*</span></label>
<div class="fieldset content"> 

//REST OF QUESTIONS HERE ....

<input class="mainForm" type="submit" name="continue" value="Save and Continue" />  

</form>

<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>

<script>
 $(document).ready(function() {

   $('#form2').validate({ // initialize the plugin      
        rules: {
           "q8[]": {
                required: true,
            },
           "q9[]": {
                required: true,
            },
            q10: {
                required: true,
            },
            q11: {
                required: true,
            },
           "q12[]": {
                required: true,
            }

        },

          errorPlacement: function(error, element) {      

           if (element.attr("type") == "radio" || element.attr("type") == "checkbox" || element.attr("name") == "q12[]") {
              error.insertAfter($(element).parents('div').prev($('question')));

           } else {
             error.insertAfter(element);             
        }
       }
    }); 
});
</script>


 </div> 
 </fieldset>
 </body>
</html>

and here is php code to insert values of page2 into database:

<?php

  try{
        $conn = new PDO('mysql:dbname=Application;host=localhost;charset=utf8', 'user', 'xxxx');
        $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $conn->prepare('UPDATE test SET q27 = :q27, q8 = :q8, q9 = :q9, q10 = :q10, q11 = :q11, q12 = :q12 WHERE username = :username');
         $stmt->execute(array(':q27' => $q27, ':q8' => $q8, ':q9' => $q9, ':q10' => $q10, ':q11' => $q11, ':q12' => $q12, ':username' => $_SESSION['SESS_USERNAME']));

}
   catch(PDOException $e) {
        echo 'Exception -> ';
        var_dump($e->getMessage());
       }

 ?>
share|improve this question

1 Answer 1

I would use the following design:

username, question, answer

That way, you can express a question with two answers as:

754703, q8, Drama
754703, q8, History
share|improve this answer
    
+1 The key here is creating a many-to-many relationship between users and answers. If answers are choices (rather than open text fields) you could even drop the question column from this table and move it to the answers table. – webbiedave Oct 13 '14 at 12:36
    
Good point, @webbiedave. – Tim Smith Oct 13 '14 at 12:42
    
@webbiedave but my problem is that 1. some of my questions are text field (Auto-completion), so I cannot set questions as columns :( 2. As I mentioned in my question, there are many questions with ,multiple answers, for example user may choose 3 items for q8, 5 item for q10, 2 item for q11, ...., I don't know if it is a good practice, since all the other answers will repeat in rows – mOna Oct 13 '14 at 13:03
    
@tim smith: actually at the beginning, I planned to have this design, but I had some problem on how to insert the array answers into db.. I also wrote my question here stackoverflow.com/questions/26242900/… but still, I couldn't fix it :( – mOna Oct 14 '14 at 10:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.