使用包含表单数据

时间:2018-05-15 12:02:31

标签: php arrays for-loop

我有一个表单,可以动态加载要从我的数据库更新的数据。第一个表单字段包含表1中的问题,第二个表单字段包含每个问题的可选答案,例如选项a,b,c,d。表格如下所示:

  

问题1

  • 选项a

  • 选项b

  • 选项c

  • 选项d

  

问题2

  • 选项a

  • 选项b

  • 选项c

  • 选项d

依此类推(动态)。在表单提交时,应更新对问题和答案的所有更改。我的问题是我无法正确地将正确格式的问题和选项保存在数组中,如何使用其唯一ID更新我的数据库。

这就是我到目前为止......

我的数据库结构

questions table

options table

myform.php

<?php

$cid = $_GET['course_id'];
$i = 0;
$z= 0;
$host   = 'localhost';
$dbname = 'einstower_elearn';
$username = "root";
$password = "";
$pdo = new PDO('mysql:dbname=einstower_elearn;host=localhost', $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$question[] = 0;
$mysqli = new mysqli("localhost", "root", "", "einstower_elearn");

$getusr = $pdo->prepare("SELECT * FROM questions WHERE eid= '$cid'");


$getusr->execute();

$count_q = $getusr->rowCount();

$getusr->setFetchMode(PDO::FETCH_ASSOC);

  while($obj = $getusr->fetch())
        {
         //Create an array for question ID 
$question[] = $obj['qid'];
}


//for each question get its options from another table
foreach ($question as $key => $value) {
  # code...

$getopt = $pdo->prepare("SELECT * FROM questions WHERE qid= '$value' AND eid='$cid'");


$getopt->execute();

$count_questions = $getopt->rowCount();

if ($count_questions>=1){

$getopt->setFetchMode(PDO::FETCH_ASSOC);

     while($obj = $getopt->fetch())
        {
          $z++;
$qid2 = $obj['qid'];
$questionName = $obj['qns'];
//form starts here
echo '
            <form method="POST" action="edit_exam_course.php" enctype="multipart/form-data">
  <input type="hidden" class="form-control" name="qid" value="'.$qid2.'">
        <div class="row form-group" style="padding-top:40px !important;">
                    <div class="col-sm-2">
                        <label class="control-label modal-label">Question</label>
                    </div>
                    <div class="col-sm-10">
    //Create an array for updated questions in form
                        <input type="text" value="'.$questionName.'" class="form-control" name="l_title[]" required>
                    </div>
                </div>';
$getopt = $pdo->prepare("SELECT * FROM options WHERE qid= '$qid2'");


$getopt->execute();

$count_mat = $getopt->rowCount();

$getopt->setFetchMode(PDO::FETCH_ASSOC);

     while($obj = $getopt->fetch())
        {
          $i++;
$optionTitle = $obj['option'];
$optionID = $obj['optionid'];
               echo '<div class="rwo form-group">
  <label class="col-md-12 control-label" for="' . $i . '1"></label>  
  <div class="col-md-12">
//option ids (unique)
  <input type="hidden" class="form-control" name="questid" value="'.$optionID.'">
  <input id="' . $i . '1" name="optTitle" value="'.$optionTitle.'" class="form-control input-md" type="text">

  </div>
</div>'; 
}}

  }elseif ($count_questions<=0) {

                   unset($question);
                   $question[] = null;
}
}
?>

            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
                <button type="submit" name="update_exam_course" class="btn btn-primary"><span class="glyphicon glyphicon-check"></span>Update</button>
            </div>

?>

edit_exam_course.php

<?php
  session_start();
  include_once('connection.php');

  if(isset($_POST['update_exam_course'])){
  //for each question i want to update the question and its options
  foreach ($_POST['l_title'] as $p) {
//query would be here
var_dump($_POST);
      }

}
?>

目前,数组的内容包含所有问题以及最后一个问题的最后一个选项。我想要的是为每个问题及其选项和如何使用此数组更新数据库的数组。非常感谢你的帮助

1 个答案:

答案 0 :(得分:0)

您没有将输入数组与问题ID相关联,并且在选项输入中您根本没有使用数组。

我在代码中添加了一些更改,包括优化 - 您每次都在循环内部查询,而不是在循环外查询一次。看看我添加的评论,以便更好地理解。

<?php
$cid = $_GET['course_id'];
$i = 0;
$z = 0;
$host = 'localhost';
$dbname = 'einstower_elearn';
$username = "root";
$password = "";
$pdo = new PDO('mysql:dbname=einstower_elearn;host=localhost', $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$mysqli = new mysqli("localhost", "root", "", "einstower_elearn");

$getusr = $pdo->prepare("SELECT * FROM questions WHERE eid= '$cid'");
$getusr->execute();

$count_q = $getusr->rowCount();

// Initialising questions array
$question = [];

while ($obj = $getusr->fetch(PDO::FETCH_ASSOC)) {
    // Adding question to array with 'qid' as a key.
    $question[$obj['qid']] = $obj;
}

// Initializing options array
$options = [];

// Selecting options
$getopt = $pdo->prepare("SELECT * FROM options WHERE qid IN ('" . implode("','", array_keys($question)) . "')");

while ($obj = $getopt->fetch(PDO::FETCH_ASSOC)) {
    // Adding option to array with 'qid' as a key.
    $options[$obj['qid']][] = $obj;
}

// Selecting questions
$getopt = $pdo->prepare("SELECT * FROM questions WHERE qid IN ('" . implode("','", array_keys($question)) . "') AND eid='$cid'");

// Looping through questions
while ($obj = $getopt->fetch(PDO::FETCH_ASSOC)) {
    $z++;
    $qid2 = $obj['qid'];
    $questionName = $obj['qns'];
//form starts here
    echo '
        <form method="POST" action="edit_exam_course.php" enctype="multipart/form-data">
            <input type="hidden" class="form-control" name="qid" value="' . $qid2 . '">
            <div class="row form-group" style="padding-top:40px !important;">
                <div class="col-sm-2">
                    <label class="control-label modal-label">Question</label>
                </div>
                <div class="col-sm-10">
                    //Create an array for updated questions in form
                    <input type="text" value="' . $questionName . '" class="form-control" name="question[' . $qid2 . ']" required>
                </div>
            </div>';

    foreach ($options[$obj['qid']] as $obj) {
        $i++;
        $optionTitle = $obj['option'];
        $optionID = $obj['optionid'];
        echo '
            <div class="rwo form-group">
                <label class="col-md-12 control-label" for="' . $i . '1"></label>  
                <div class="col-md-12">
                    //option ids (unique)
                    <input id="' . $i . '1" name="optTitle[' . $qid2 . '][' . $optionID . ']" value="' . $optionTitle . '" class="form-control input-md" type="text">
                </div>
            </div>';
    }
}
?>

<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span>
        Cancel
    </button>
    <button type="submit" name="update_exam_course" class="btn btn-primary"><span
                class="glyphicon glyphicon-check"></span>Update
    </button>
</div>

在ajax页面中执行:

<?php
var_dump($_POST);

<?php
print_r($_POST);

要了解结果并相应地制作代码。