【问题标题】:How to insert multiple value onto one column with different rows?如何将多个值插入具有不同行的一列?
【发布时间】:2017-04-21 23:51:42
【问题描述】:

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

致命错误:带有消息的未捕获异常“PDOException”
'SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 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>

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

}
?>

【问题讨论】:

    标签: php html mysql


    【解决方案1】:

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

    $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')");
    

    【讨论】:

    • 如何存储文本的输入类型,因为使用“ans”是存储单选按钮的值?
    • 它在 option_answer 列中打印了 8 次这个 ":ans"。我想知道这里有什么错误。
    • 我有任何问题是我如何根据我为 question_text 插入的 question_id 插入 option_answer?
    • 第一个问题就是这样解决的 $stmt2 = $conn->prepare("INSERT INTO option_tbl(option_answer) VALUES(:ans)");
    • “我如何根据我为 question_text 插入的 question_id 插入 option_answer”如果您现在插入问题记录,如果问题是以前保存的,则可以使用 last insert id记录,您必须获取它的 id
    【解决方案2】:

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

    【讨论】:

    • 我仍然无法存储输入值文本。它导致列中有 8 行 ":ans"。
    猜你喜欢
    • 2020-03-11
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    相关资源
    最近更新 更多