0

我有一个包含 5 页(多页)的 php 表单。有 27 个问题,其中许多是多个值。我将所有答案插入到一个表“测试”中,并且在一行中插入了多个值,因此每一行代表与一个用户相关的答案。这是表“测试”:

在此处输入图像描述

问题:

我想在不同的行中有多个值。例如,对于 q8,我需要将戏剧和历史放在不同的行中,而不是用逗号分隔的一行!。但是,我不知道这怎么可能,因为我有 27 个问题,其中 17 个是多个值,用户可以为其插入无限数量的答案(例如,用户可以为 q8 选择 10 种类型)。

这是“page2.php”页面之一的html代码(因为有5个页面,我无法粘贴所有代码,我只是粘贴page2作为示例,其他页面几乎相同)。

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

这是将 page2 的值插入数据库的 php 代码:

<?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());
       }

 ?>
4

1 回答 1

2

我会使用以下设计:

username, question, answer

这样,您可以用两个答案表达一个问题:

754703, q8, Drama
754703, q8, History
于 2014-10-13T12:30:37.740 回答