[英]How to insert multiple value onto one column with different rows?
I'm trying to store the question text and answer into different table. 我正在尝试将问题文本存储到不同的表格中。 First I can stored the question text value, but subsequently when I insert the value for answer, it display
首先我可以存储问题文本值,但随后当我插入答案值时,它会显示
Fatal error: Uncaught exception 'PDOException' with message
致命错误:带有消息的未捕获异常“PDOException”
'SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1''SQLSTATE [21S01]:插入值列表与列列表不匹配:1136列数与第1行的值计数不匹配
My objective is to store the question text on question table and answer in option table. 我的目标是将问题文本存储在问题表中并在选项表中回答。 I have tried a few method, but still can't get it to work.
我尝试了一些方法,但仍然无法让它工作。 Those commented line are some of my methods.
那些注释行是我的一些方法。
HTML code: 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>
PHP code: 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();*/
}
?>
First of all , you do not store multiple values in one column. 首先,您不会在一列中存储多个值。 You store them in multple rows.
您将它们存储在多行中。 The error you have comes directly from this
您遇到的错误直接来自于此
$stmt2 = $conn->prepare("INSERT INTO `option_tbl`(option_answer) VALUES(':ans1', ':ans2', 'ans3', 'ans4')");
You are telling sql that you want to store something in the option_answer
column but you are sending 4 different values. 您告诉sql您要在
option_answer
列中存储某些内容,但是您要发送4个不同的值。
it should look something like this: 它应该看起来像这样:
$stmt2 = $conn->prepare("INSERT INTO `option_tbl`(option_answer) VALUES(:ans)");
Then 然后
$stmt2->bindParam(":ans",$anstext1);
$stmt2->execute()
...
$stmt2->bindParam(":ans",$anstext4);
$stmt2->execute()
This still doesn't get you out of the woods. 这仍然不能让你走出困境。 How do you know which question this answer relates to?
你怎么知道这个答案涉及哪个问题? YOu probably want something like
你可能想要这样的东西
$stmt2 = $conn->prepare("INSERT INTO `option_tbl`(question, option_answer) VALUES(':question, :ans')");
@e4c5 explained it well enough why this is not working.... @ e4c5很好地解释了为什么这不起作用....
Try it like (You may also want to store question id else this will be of no use...Assuming you can edit it yourself according to your need...) 试试吧(您可能还想存储问题ID,否则这将毫无用处......假设您可以根据需要自行编辑...)
<?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();*/
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.