Php 如何将多个值插入到具有不同行的一列中?
我试图将问题文本和答案存储到不同的表中。首先我可以存储问题文本值,但随后当我插入回答值时,它会显示出来 致命错误:未捕获异常“PDOException”,并显示消息Php 如何将多个值插入到具有不同行的一列中?,php,html,mysql,Php,Html,Mysql,我试图将问题文本和答案存储到不同的表中。首先我可以存储问题文本值,但随后当我插入回答值时,它会显示出来 致命错误:未捕获异常“PDOException”,并显示消息 'SQLSTATE[21S01]:插入值列表与列列表不匹配:1136列计数与第1行的值计数不匹配' 我的目标是将问题文本存储在问题表中,并将答案存储在选项表中。我试过几种方法,但还是不能奏效。这些注释行是我的一些方法 HTML代码: <div class="container"> <button type="b
'SQLSTATE[21S01]:插入值列表与列列表不匹配:1136列计数与第1行的值计数不匹配' 我的目标是将问题文本存储在问题表中,并将答案存储在选项表中。我试过几种方法,但还是不能奏效。这些注释行是我的一些方法 HTML代码:
<div class="container">
<button type="button" class="btn btn-success" onclick="goBack()">
<span class="glyphicon glyphicon-arrow-left"></span> Back
</button><br><br>
<p></p>
<form class="table" method="post" id="mcq-form">
<table class="table">
<tbody>
<tr>
<td>Question:</td>
<td><input type="text" size="80" name="questiontext"></td>
</tr>
<tr>
<td>1. </td>
<td><input type="text" size="70" name="ans1"><input value="1" name="ans" type="radio"></td>
</tr>
<tr>
<td>2. </td>
<td><input type="text" size="70" name="ans2"><input value="2" name="ans" type="radio"></td>
</tr>
<tr>
<td>3. </td>
<td><input type="text" size="70" name="ans3"><input value="3" name="ans" type="radio"></td>
</tr>
<tr>
<td>4. </td>
<td><input type="text" size="70" name="ans4"><input value="4" name="ans" type="radio"></td>
</tr>
<tr>
<td align="center"><input type="submit" name="submit" value="Create"></td>
</tr>
</tbody>
</table></form>
</div>
返回
问题:
1.
2.
3.
4.
PHP代码:
<?php
require_once 'dbConn.php';
if(!empty($_POST{'submit'})) {
//$questiontext = $_POST['questiontext'];
$anstext1 = $_POST['ans1'];
$anstext2 = $_POST['ans2'];
$anstext3 = $_POST['ans3'];
$anstext4 = $_POST['ans4'];
//$radiobtn = $_POST['ans'];
//add the first record into question table
/*$stmt1 = $conn->prepare("INSERT INTO `question`(question_text) VALUES(:questiontext)");
$stmt1->bindParam(":questiontext",$questiontext);
$stmt1->execute();*/
//$answerArray = array["$anstext1", "$anstext2", "$anstext3", "$anstext4"];
/*$value = array(':ans1', 'ans2', ':ans3', 'ans4');
for($i=0; $i<=count($value); $i++) {
$i = $value[$i];
}*/
$stmt2 = $conn->prepare("INSERT INTO `option_tbl`(option_answer) VALUES(':ans1', ':ans2', 'ans3', 'ans4')");
$stmt2->bindParam(":ans1",$anstext1);
$stmt2->bindParam(":ans2",$anstext2);
$stmt2->bindParam(":ans3",$anstext3);
$stmt2->bindParam(":ans4",$anstext4);
$stmt2->execute();
//$stmt2->execute();
/*$conn->beginTransaction();
//insert first query to question table
$questionsql = ("INSERT INTO `question`(question_text) VALUES(:questiontext)");
$q = $conn->prepare($questionsql);
$q->bindValue(":questiontext",$questiontext);
$q->execute();
//insert second query to option table
$answersql = ("INSERT INTO `option_tbl`(option_answer) VALUES(:ans1, :ans2, :ans3,:ans4)") ;
$a = $conn->prepare($answersql);
$a->bindValue("anstext1",$anstext1);
$a->bindValue("anstext2",$anstext2);
$a->bindValue("anstext3",$anstext3);
$a->bindValue("anstext4",$anstext4);
$a->execute();
$conn->commit();*/
}
?>
首先,不能在一列中存储多个值。您可以将它们存储在多行中。您的错误直接来自于此
$stmt2 = $conn->prepare("INSERT INTO `option_tbl`(option_answer) VALUES(':ans1', ':ans2', 'ans3', 'ans4')");
您告诉sql您想在选项\u answer
列中存储一些内容,但您发送了4个不同的值
它应该是这样的:
$stmt2 = $conn->prepare("INSERT INTO `option_tbl`(option_answer) VALUES(:ans)");
然后
这仍然不能让你走出困境。你怎么知道这个答案与哪个问题有关?你可能想要像这样的东西
$stmt2 = $conn->prepare("INSERT INTO `option_tbl`(question, option_answer) VALUES(':question, :ans')");
@e4c5很好地解释了为什么这不起作用
像这样尝试(您可能还想存储问题id,否则这将没有用…假设您可以根据需要自己编辑…)
由于使用“ans”存储单选按钮的值,我如何存储文本的输入类型?它在选项回答列中打印了此“:ans”的8倍。我想知道这里的错误是什么。我得到的问题是,如何根据我为问题文本插入的问题id插入选项答案?第一个问题通过以下$stmt2=$conn->prepare(“插入到选项tbl
(选项答案)值(:ans)”解决;“如何插入选项\根据我为问题\文本插入的问题\ id回答”如果您现在插入问题记录,您可以使用,如果问题是以前保存的记录,您将必须获取其idI,但仍然无法存储输入值文本。它在列中产生了8行“:ans”。
$stmt2 = $conn->prepare("INSERT INTO `option_tbl`(question, option_answer) VALUES(':question, :ans')");
<?php
require_once 'dbConn.php';
if(!empty($_POST{'submit'})) {
//$questiontext = $_POST['questiontext'];
$answers[] = $_POST['ans1'];
$answers[] = $_POST['ans2'];
$answers[] = $_POST['ans3'];
$answers[] = $_POST['ans4'];
//$radiobtn = $_POST['ans'];
//add the first record into question table
/*$stmt1 = $conn->prepare("INSERT INTO `question`(question_text) VALUES(:questiontext)");
$stmt1->bindParam(":questiontext",$questiontext);
$stmt1->execute();*/
//$answerArray = array["$anstext1", "$anstext2", "$anstext3", "$anstext4"];
/*$value = array(':ans1', 'ans2', ':ans3', 'ans4');
for($i=0; $i<=count($value); $i++) {
$i = $value[$i];
}*/
foreach( $answers as $answer){
$stmt2 = $conn->prepare("INSERT INTO `option_tbl`(option_answer) VALUES(':ans')");
$stmt2->bindParam(":ans",$answer);
$stmt2->execute();
//$stmt2->execute();
}
/*$conn->beginTransaction();
//insert first query to question table
$questionsql = ("INSERT INTO `question`(question_text) VALUES(:questiontext)");
$q = $conn->prepare($questionsql);
$q->bindValue(":questiontext",$questiontext);
$q->execute();
//insert second query to option table
$answersql = ("INSERT INTO `option_tbl`(option_answer) VALUES(:ans1, :ans2, :ans3,:ans4)") ;
$a = $conn->prepare($answersql);
$a->bindValue("anstext1",$anstext1);
$a->bindValue("anstext2",$anstext2);
$a->bindValue("anstext3",$anstext3);
$a->bindValue("anstext4",$anstext4);
$a->execute();
$conn->commit();*/
}
?>