【问题标题】:How to insert correct values in database [closed]如何在数据库中插入正确的值[关闭]
【发布时间】:2012-10-11 21:44:53
【问题描述】:

我在这里有一个 jsfiddle:http://jsfiddle.net/ybZvv/58/

请按照小提琴中的步骤操作:

1:当你打开小提琴时,点击“添加问题”按钮两次,这将追加 2 行。

2:在第一行选择答案按钮“A”和“C”,在第二行选择答案按钮“A”、“B”和“E”。选择的每个答案按钮的文本输入值显示在下方。

现在我要做的是将问题编号和答案值发布到数据库中。

发布后的数据库应如下所示:

问题表:

QuestionId (Question Number)
1
2

答案表:

AnswerId (auto)  QuestionId  Answer
1                1           A
2                1           C
3                2           A
4                2           B
5                2           E

我的问题是,如何在下面的 mysqli 代码中发布答案和正确的问题编号,以便在“问题”和“答案”表中插入这些答案和问题编号?

下面我已经设置了 mysqli/php 代码,但它需要重新调整,以便它可以正确插入答案和相关的问题编号。

$i = 0;
$c = count($_POST['numQuestion']); //count number of rows

for($i = 0;  $i < $c; $i++ ){

$questionsql = "INSERT INTO Question (QuestionId) 
    VALUES (?)";


    if (!$insert = $mysqli->prepare($questionsql)) {
      // Handle errors with prepare operation here
    }

$insert->bind_param("i", $_POST['numQuestion'][$i]);

        $insert->execute();

        if ($insert->errno) {
          // Handle query error here
        }

        $insert->close();

        $lastID = $mysqli->insert_id;

         $answersql = "INSERT INTO Answer (QuestionId, Answer) 
    VALUES (?, ?, ?)";

      if (!$insertanswer = $mysqli->prepare($answersql)) {
      // Handle errors with prepare operation here
    }  


    $insertanswer->bind_param("is", $lastID, $_POST['value'][$i]);

        $insertanswer->execute();

        if ($insertanswer->errno) {
          // Handle query error here
        }

        $insertanswer->close();

}

我已经为上述场景做了一个 var_dump($_POST),它的输出是这样的:

array(2) { 
            ["numQuestion"]=> array(2) { 
                                        [0]=> string(1) "1" 
                                        [1]=> string(1) "2" 
                                       }
           ["submitDetails"]=> string(14) "Submit Details" 
           ["value"]=> array(4) { 
                                    ["answerARow"]=> string(1) "A" 
                                    ["answerCRow"]=> string(1) "C" 
                                    ["answerBRow"]=> string(1) "B" 
                                    ["answerERow"]=> string(1) "E" 
                                } 
        }

我收到 2 个相同的错误:

警告:mysqli_stmt::execute(): (23000/1048): 列 'Answer' 不能 在第 257 行的 /.../ 中为 null 警告:mysqli_stmt::execute(): (23000/1048):第 257 行 /.../ 中的“答案”列不能为空

更新:

我已经更新了 fiddle 以包含多维数组,抱歉我忘了把它放进去,但代码行如下:

var $newBtn = $(("<input class='answerBtnsRow answers' type='button' style='display:%s;' onclick='btnclick(this, " + gQuestionIndex + ");' />").replace('%s',$this.is(':visible')?'inline-block':'none')).attr('name', "value[" + gQuestionIndex + "][]").attr('value', $this.val()).attr('class', $this.attr('class')).attr('id', $this.attr('id')+'Row'); 

我的建议是使用这种格式的多维数组:value[n][],其中 n 是问题编号。使用这个新设置,您应该最终得到以下输入字段:

<input type="hidden" value="A" name="value[1][]">
<input type="hidden" value="B" name="value[1][]">
<input type="hidden" value="A" name="value[2][]">
<input type="hidden" value="C" name="value[2][]">
<input type="hidden" value="E" name="value[2][]">

请注意,所选值已编码在 value 属性中。 name 属性只包含值所属的问题。

【问题讨论】:

  • 帖子似乎没有所有答案。此外,发布的答案与发布的问题 ID 之间没有关系。你能解释一下你使用的帖子逻辑吗?
  • @Mt.Schneiders 老实说,我试图做的是计算问题的数量以检索问题编号,然后对于每个计数行,显示每个计数行的答案数组。但显然这是行不通的。我相信但我需要帮助的最好方法是,当您选择答案按钮时,它会显示一个文本输入,显示答案的值。每个文本输入都包含一个名称属性,该属性包含一个多维数组,其中将说明答案值是什么以及它所属的问题编号,如value[n][]。您可以在 jsfiddle 中查看此内容
  • (1, 2) VALUES (1, 2, 3) - 其中一件不一样,其中一件不完全相同。
  • @Mt.Schneiders value[n][] 其中n是问题编号,答案的ID显示在[]
  • 请不要在您的问题过程中使用 JSfiddle 链接,如果该链接失效,您的问题将变得毫无用处。尝试将您的问题简化为最基本的问题。

标签: php javascript jquery sql mysqli


【解决方案1】:

$POST 似乎没有所有信息,我建议您更改帖子结构以将问题与答案联系起来,如下所示:

array(2) { 
            ["numQuestion"]=> array(2) { 
                                        [0]=> string(1) "1" 
                                        [1]=> string(1) "2" 
                                       }
           ["submitDetails"]=> string(14) "Submit Details" 
           ["question_1"] => array(2) {
                                        [0] => string(1) "A"
                                        [1] => string(1) "C"
                                      }
           ["question_2"] => array(2) {
                                        [0] => string(1) "A"
                                        [1] => string(1) "B"
                                        [2] => string(1) "E"
                                      }
        }

现在,如果帖子具有这种结构,您可以执行以下操作来插入数据:

$numQuestionArray = $_POST["numQuestion"];

for($i =0; $i < count($numQuestionArray);$i++)
{
    $questionId = $numQuestionArray[$i];

    $questionsql = "INSERT INTO Question (QuestionId) VALUES (?)";

    if (!$insert = $mysqli->prepare($questionsql)) {
      // Handle errors with prepare operation here
    }

    $insert->bind_param("i", $questionId);

    $insert->execute();

    if ($insert->errno) {
      // Handle query error here
    }

    $insert->close();

    $lastID = $mysqli->insert_id;

    $questionAnswerPostStr = "question_".$questionId;

    $answerArray = $_POST[$questionAnswerPostStr];

    for($j =0; $j < count($numQuestionArray);$j++)
    {
        $answer = $numQuestionArray[$j];

        $answersql = "INSERT INTO Answer (QuestionId, Answer) VALUES (?, ?)";

        if (!$insertanswer = $mysqli->prepare($answersql)) {
          // Handle errors with prepare operation here
        }  

        $insertanswer->bind_param("is", $lastID, $answer);

        $insertanswer->execute();

        if ($insertanswer->errno) {
          // Handle query error here
        }

        $insertanswer->close();
    }
}

但是,我仍然认为 question_id 应该在表上自动递增,而不是插入 $POST 值。

【讨论】:

  • 嗨,我试过你的方法,但我仍然得到这个结构:array(2) { ["numQuestion"]=&gt; array(2) { [0]=&gt; string(1) "1" [1]=&gt; string(1) "2" } ["submitDetails"]=&gt; string(14) "Submit Details" ["value"]=&gt; array(3) { ["answerARow"]=&gt; string(1) "A" ["answerCRow"]=&gt; string(1) "C" ["answerERow"]=&gt; string(1) "E" } }
  • 该方法不适用于此结构,它仅适用于建议的结构。你必须改变你的脚本以采取不同的行动。
  • 我想知道您是否在问题下方看到我关于如何检索每个答案和问题编号的评论,这行不通吗?你见过小提琴吗?
  • 我做到了,但我真的不明白它是如何工作的,并且查看您的代码,它只会插入 2 个答案,因为循环会迭代问题的数量。而且,我不知道如何使用该结构检索问题答案。虽然我可能只是错过了一些东西。因此,您可以尝试我的方法或更新您的问题,例如解释(使用代码)如何从给定问题中检索所有答案。
  • 我更新了 fiddle,抱歉我忘了在多维数组的 fiddle 中包含一行代码。我在我的问题中包含了一个更新部分,解释了我希望多维数组如何检索答案以及与每个答案相关的问题编号
猜你喜欢
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 2012-12-25
  • 2016-04-06
相关资源
最近更新 更多