2012-10-12 96 views
0

我想使用這種格式的多維數組:value [n] [],其中n是問題編號。使用此新設置,您應該以下面的複選框字段結束:如何正確發佈和插入數據到數據庫中?

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

請注意,所選值將在值屬性中編碼。 name屬性只包含值所屬的問題。

那麼,上述輸入指出的是:

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

我想這些細節插入「問題」和下面的「答案」的數據庫表:

問表:

SessionId QuestionId 

MUL    1 
MUL    2 

回答表格:

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 

現在我已經嘗試編寫下面的mysqli/php代碼來將這些值插入到數據庫中,但我正在接收錯誤,並且想要實現我想實現的目標時失敗。我需要幫助才能在相關表格中正確插入正確的值。

以下爲PHP/mysqli的代碼:

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 = $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(); 

     $insert->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(); 


} 

} 

} 

var_dump($_POST)輸出該下面:

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

下面是我收到錯誤和每個錯誤被鏈接到的代碼行:

警告:在第252行的/.../中爲foreach()提供了無效參數

foreach($value as $answer) { 

警告:mysqli_stmt ::執行():(一千〇六十二分之二萬三):重複條目 'MUL-0' 的鍵 '主要' 在上線/.../ 242

上述錯誤顯示,被插入毫無疑問號,因爲它使顯示爲「0」

+0

你正在做外的foreach,通過循環'$ _ POST [「值」]'和分配鍵'$ id'和值到'$價值'。所以'$ id's是「answerARow」,「answerCRow」等,「$ value」是「A」,「C」,「B」和「E」。顯然你不能循環一個字符串,因此foreach錯誤。 – Travesty3

+1

您的$ _POST與您的表單不符? 「answerARow」類型鍵從哪裏來的/ – ernie

+0

@ernie answerARow來自用戶選擇的答案按鈕:我在這裏有一個jsfiddle,你可以看看我的應用程序是什麼樣的:jsfiddle.net/ybZvv/59請按照步驟在小提琴中:1:當你打開小提琴時,點擊「添加問題」按鈕兩次,這將追加2行。 2:在第一行中選擇答案按鈕「A」和「C」,在第二行中選擇答案按鈕「A」,「B」和「E」。下方顯示所選每個答案按鈕的文本輸入值。 – user1701484

回答

0

因此,它看起來就像你正在試圖做的主要事情是讓周圍的無力皮膚複選框。我以前做過這個。我這樣做的方式是使用與隱藏複選框綁定的範圍。

我使用了跨度,因爲我不確定表單域中的按鈕是否會在提交表單時發佈值。我知道跨度不會。這保證只會提交您的複選框值,而不是「掩蓋」值。

在你的jsfiddle,在updateAnswer()功能,你有這樣的代碼:

if (!bDisableAppend) { 
    // append those values to the form 
    var input = '<input type="checkbox" id="' + hid + '" name="value[' + id + ']" checked /><label for="' + hid + '">' + value + '</label>'; 
    _oCurrAnswerContainer.append(input); 
} 

在該代碼中,你喜歡value[answerARow]複選框的name屬性設置的東西。這是你的問題所在。

TBH,對我來說很難遵循你的代碼,所以我想出的解決方案可能需要一些調整來添加我在示例中錯過的任何隱藏技巧。但這裏是我想出了:

jsFiddle

JS/jQuery的:

var options = ["A", "B", "C", "D", "E", "F", "G", "H"]; 


// this will bind the event handler to the document element, so that even when a new element is created, 
// the event will be automatically bound to it. 
$(document).on("click", "span.checkbox", function() { 
    $("#chk"+ $(this).attr("id")).click(); 
    $(this).toggleClass("unselected selected"); 
}); 


$("#btnAddQuestion").click(function() { 
    var questionNum = $("#tblQuestions tbody tr").length+1; 

    var cell = $(document.createElement("td")).addClass("optAns").html("Answer:<br />"); 

    // loop through the options and add them to the cell 
    for (var i in options)  
    { 
     $(cell).append(
      // the front-end overlay for the checkbox 
      $(document.createElement("span")) 
       .addClass("checkbox unselected") 
       .attr("id", "Value"+ questionNum + options[i]) 
       .html(options[i]) 
     ).append(
      // the hidden checkbox that will post the actual value 
      $(document.createElement("input")) 
       .attr("type", "checkbox") 
       .addClass("hidden") 
       .attr("id", "chkValue"+ questionNum + options[i]) 
       .attr("name", "value["+ questionNum +"][]") 
       .attr("value", options[i]) 
     ); 
    } 

    // create a new table row, append the "option and answer" cell 
    // and question number cell, and append it to the table body 
    $(document.createElement("tr")) 
     .append(cell) 
     .append($(document.createElement("td")).addClass("questionNum").html(questionNum)) 
     .appendTo("#tblQuestions tbody"); 
}); 

HTML:

<div id="detailsBlock"> 
    <button id="btnAddQuestion">Add Question</button> 
</div> 
<hr /> 
<div id="details"> 
    <table id="tblQuestions"> 
     <thead> 
      <tr> 
       <th>Option and Answer</th> 
       <th>Question No</th> 
      </tr> 
     </thead> 
     <tbody> 
     </tbody> 
    </table> 
</div> 
+0

我已經設置了「SessionId」和「QuestionId」作爲主鍵我有一個複合鍵。你不能在同一個考試中有同樣的問題編號,所以他們爲什麼都是主鍵 – user1701484

+0

問題是我怎樣才能得到表單來產生數據lol。這就是我正在努力與 – user1701484

+0

@ user1701484:我改變了我的答案。看一看。確保你看看[jsFiddle](http://jsfiddle.net/eg5QU/3/)。要查看複選框發生了什麼,只需在CSS中爲'.hidden'類註釋'display:none;'即可。 – Travesty3

0

您當前的代碼生成這個(注意壞名字屬性):

<input type="button" onclick="btnclick(this, 1);" style="display:inline-block;" class="answerBtns answers answerBtnsOff" name="value[answerF]" value="F" id="answerHRow"> 

注意,name屬性是value[answerF],而不是因爲你在你的問題寫了value[1][]

更新JavaScript和改變$newBtn行:

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'); 
+0

乾杯問題是,雖然我仍然在PHP中得到相同的錯誤。你知道我需要改變在PHP代碼? – user1701484

+0

什麼是$ _POST節目?此外,你有太多的問題,這是一個問題。我建議爲數據庫錯誤和foreach錯誤創建單獨的問題。 – ernie

+0

好吧,對於這個問題,只關注foreach錯誤。我將更新var_dump($ _ POST),給我5分鐘 – user1701484

相關問題