如何将多个值插入到具有不同行的一列上?

时间:2016-12-07 06:16:13

标签: php html mysql

我正在尝试将问题文本存储到不同的表格中。首先我可以存储问题文本值,但随后当我插入答案值时,它会显示

  

致命错误:带有消息
的未捕获异常'PDOException'   'SQLSTATE [21S01]:插入值列表与列列表不匹配:1136列数与第1行的值计数不匹配

我的目标是将问题文本存储在问题表中并在选项表中回答。我尝试了一些方法,但仍然无法让它工作。那些注释行是我的一些方法。

HTML code:

<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代码:

<?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();*/

}
?>

2 个答案:

答案 0 :(得分:3)

首先,您不会在一列中存储多个值。您将它们存储在多行中。您遇到的错误直接来自此

$stmt2 = $conn->prepare("INSERT INTO `option_tbl`(option_answer) VALUES(':ans1', ':ans2', 'ans3', 'ans4')");

您告诉sql您要在option_answer列中存储某些内容,但是您要发送4个不同的值。

看起来应该是这样的:

$stmt2 = $conn->prepare("INSERT INTO `option_tbl`(option_answer) VALUES(:ans)");

然后

$stmt2->bindParam(":ans",$anstext1);
$stmt2->execute()
...
$stmt2->bindParam(":ans",$anstext4);
$stmt2->execute()

这仍然不能让你走出困境。你怎么知道这个答案涉及哪个问题?你可能想要像

这样的东西
$stmt2 = $conn->prepare("INSERT INTO `option_tbl`(question, option_answer) VALUES(':question, :ans')");

答案 1 :(得分:0)

@ e4c5解释了为什么这不起作用....

尝试一下(您可能还想存储问题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();*/

}
?>