gpt4 book ai didi

php - 尝试在一个查询中将数据插入 MySQL 表,但更改一个特定列

转载 作者:行者123 更新时间:2023-12-02 00:47:29 24 4
gpt4 key购买 nike

我正在尝试 INSERT使用 SQL 语句将一些数据放入我的表中。一个用户完成了一个有 10 个问题的调查,然后我想 INSERT使用一条 SQL 语句将这 10 个答案存入数据库。所以基本上唯一会改变的 2 个字段是 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>

submit-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, $10
);
$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

最佳答案

$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` = ?");

关于php - 尝试在一个查询中将数据插入 MySQL 表,但更改一个特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60599967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com