尝试在一个查询中将数据插入 MySQL table,但更改了一个特定的列

Trying to INSERT data into MySQL table in one query, yet changing one specific column

我正在尝试使用 SQL 语句 INSERT 一些数据到我的 table 中。一个用户完成了一个有 10 个问题的调查,然后我想 INSERT 使用一个 SQL 语句将这 10 个答案输入数据库。所以基本上只有 questionIDanswer 列会发生变化?请注意,我刚刚在 <form> 标签中给出了 2 个问题作为示例以获取信息。

问卷调查:

<!-- Attempt 2 of questionnaire -->
<form action="scripts/submit-survey.php" method="post">
    <?php
        $questionnaire = "Welcome Questionnaire";
        $questionID = 1;
        $stmt = $conn->prepare ("SELECT `questionnaire`.`questionnaireName`, `questionnaireQuestions`.`questionID`,`question`.`question` FROM `questionnaire` INNER JOIN `questionnaireQuestions` ON `questionnaire`.`questionnaireID` = `questionnaireQuestions`.`questionnaireID` INNER JOIN `question` ON `questionnaireQuestions`.`questionID` = `question`.`questionID` WHERE `questionnaire`.`questionnaireName` = ? AND `question`.`questionID` = ?");
        $stmt->bind_param("si", $questionnaire, $questionID);
        $stmt->execute();
        $result = $stmt->get_result();
        while($row = $result -> fetch_assoc())  
            {
    ?>
    <p><?php echo $row['question']; ?></p>
    <?php
            }
    ?>
    <label><input type="radio" name="q1" value="1"> 1</label>
    <br>
    <label><input type="radio" name="q1" value="2"> 2</label>
    <br>
    <label><input type="radio" name="q1" value="3"> 3</label>
    <br>

    <?php
        $questionnaire = "Welcome Questionnaire";
        $questionID = 2;
        $stmt = $conn->prepare ("SELECT `questionnaire`.`questionnaireName`, `questionnaireQuestions`.`questionID`,`question`.`question` FROM `questionnaire` INNER JOIN `questionnaireQuestions` ON `questionnaire`.`questionnaireID` = `questionnaireQuestions`.`questionnaireID` INNER JOIN `question` ON `questionnaireQuestions`.`questionID` = `question`.`questionID` WHERE `questionnaire`.`questionnaireName` = ? AND `question`.`questionID` = ?");
        $stmt->bind_param("si", $questionnaire, $questionID);
        $stmt->execute();
        $result = $stmt->get_result();
        while($row = $result -> fetch_assoc())  
            {
    ?>
    <p><?php echo $row['question']; ?></p>
    <?php
            }
    ?>
    <label><input type="radio" name="q2" value="1"> 1</label><br>
    <label><input type="radio" name="q2" value="2"> 2</label><br>
    <label><input type="radio" name="q2" value="3"> 3</label><br>
    <input type="submit" value="Submit">
</form>

提交-survey.php

<?php
  require 'db.php';
  session_start();
  $seshID = $_SESSION['studentID'];

    $a1 = $_POST['q1'];
    $a2 = $_POST['q2'];
    $a3 = $_POST['q3'];
    $a4 = $_POST['q4'];
    $a5 = $_POST['q5'];
    $a6 = $_POST['q6'];
    $a7 = $_POST['q7'];
    $a8 = $_POST['q8'];
    $a9 = $_POST['q9'];
    $a10 = $_POST['q10'];
    $answer_bank = array(
      $a1, $a2, $a3, $a4, $a5, $a6, $a7, $a8, $a9, 
    );
    $stmt = $conn->prepare ("INSERT INTO `studentAnswer` (`studentAnswerID`, `studentID`, `questionID`, `answer`) VALUES (NULL, ? , ? ,?)");
          //echo $i;
          //echo ${"a". $i};
            $stmt->bind_param("iii", $seshID , $questionID , $a);
            foreach ($answer_bank as $a_b) {
              list($a) = $a_b;
              $stmt->execute();
          }
            $result = $stmt->get_result();

?>

DB Table:

您缺少 $questionID 的 1 个值。

尝试

$questionID = 1;
foreach ($answer_bank as $a) {
     $stmt->execute();
     $questionID++;
}

使用别名改进您的查询

$stmt = $conn->prepare ("SELECT 
          `qn`.`questionnaireName`,
          `qQ`.`questionID`,
          `q`.`question`
      FROM `questionnaire` `qn`
      INNER JOIN `questionnaireQuestions` `qQ` ON `qn`.`questionnaireID` = `qQ`.`questionnaireID`
      INNER JOIN `question` `q` ON `qQ`.`questionID` = `q`.`questionID` 
      WHERE `qn`.`questionnaireName` = ? AND `q`.`questionID` = ?");