dougu5950 2012-10-12 13:20
浏览 6
已采纳

苦苦于将值正确插入数据库

I want use a multi-dimensional array in this format: value[n][], where n is the question number. With this new setup, you should end up with the following input fields:

<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][]">

Note that the selected value is encoded in the value attribute. The name attribute only contains the question to which the value belongs.

So what the above inputs are stating is this:

question 1: answer: A
question 1: answer: B
question 2: answer: A
question 2: answer: C
question 2: answer: E

I want to insert these details into "Question" and "Answer" database tables below:

Question Table:

SessionId    QuestionId

MUL             1
MUL             2

Answer Table:

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

Now I have attempted writing the mysqli/php code below to insert these values into the database but I am receiving errors and failing badly in wanting to acheive what I want to achieve. I need help being able to correctly insert the correct values in the relevant tables.

Below is the php/mysqli code:

var_dump($_POST);  

$i = 0;
$c = count($_POST['numQuestion']);

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

/*
    switch ($_POST['gridValues'][$i]){

    case "3": 
    $selected_option = "A-C";
    break;

    case "4": 
    $selected_option = "A-D";
    break;

    case "5": 
    $selected_option = "A-E";
    break;

    default:
    $selected_option = "";
    break;

    }   

    */ needed later on when I insert grid values   



$results = $_POST['value'];
foreach($results as $id => $value) {
$answer = implode(':', $value);

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

    $sessid =  $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '');


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

$insert->bind_param("si", $sessid, $id);

        $insert->execute();

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

        $insert->close();

        $lastID = $id->insert_id;

        foreach($value as $answer) {

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

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

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

        $insertanswer->execute();

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

        $insertanswer->close();


}

}

}

The var_dump($_POST) outputs this below:

array(3) {
["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" }
}

Below are the errors I am receiving and the line of code each error is linked to:

Warning: implode(): Invalid arguments passed in /.../ on line 226

$answer = implode(':', $value);

Notice: Trying to get property of non-object in /.../ on line 250

$lastID = $id->insert_id;

Warning: Invalid argument supplied for foreach() in /.../ on line 252

foreach($value as $answer) {

Warning: mysqli_stmt::execute(): (23000/1062): Duplicate entry 'MUL-0' for key 'PRIMARY' in /.../ on line 242

The Above Error Points to the $insert query

MORE INFO:

The structure I really want to acheive after posting the text inputs is below:

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

When the user posts the text input values, it should insert the question numbers and the answers which belongs to their relevant question number.

  • 写回答

1条回答 默认 最新

  • drox90250557 2012-10-12 13:28
    关注

    A few things:

    1. $result / $_POST['value'] is a one-dimensional array, so $value in your foreach loop is a string that you cannot use implode on;
    2. $lastID = $id->insert_id; should be $insert->insert_id and I'm not sure if you can close the connection already before you call that statement;
    3. $value is a string (see first point) so you cannot use foreach there (third warning).
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序