0
 $answersql = "INSERT INTO Penalty_Marks (PenaltyAnswer, PenaltyMarks, QuestionId) 
    VALUES (?, ?, ?)";

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

   $c = count($_POST['incorrect']);

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


$incorrect = $_POST['incorrect'][$i];
$answerMarks = $_POST['answerMarks'][$i];
$numQuestion = $_POST['numQuestion'][$i];

$insertanswer->bind_param('sii', $incorrect, $answerMarks, $numQuestion);
$insertanswer->execute();

}

上面的插入并不是将所有不正确的答案都插入到表格中,它只是在每个问题中插入一个不正确的答案。我究竟做错了什么?

下面是当前数据库表的样子:

PenaltyAnswerId PenaltyAnswer  PenaltyMarks  QuestionId
1               A              4             12
2               D              1             13

下面是它应该是什么样的:

PenaltyAnswerId PenaltyAnswer  PenaltyMarks  QuestionId
1               A              4             12
2               B              3             12
3               D              0             12
4               D              1             13
5               E              1             13

下面是表格:

<form id="PenaltyMarks" action="insertpenaltymarks.php" method="post">

    <table id='penaltytbl'>
    <?php

    foreach($ques_ans as $questionId => $inc_ans)
    {
        $q_row_span = count($inc_ans);
        $row_count = 0;
        $inc_ans = array_values($inc_ans);

    ?>

    <tr class="questiontd">
    <td>
    <input type="hidden" name="numQuestion[]" value="<?php echo$questionId?>" />
    </td>

    <td>
    <input type="hidden" class="hiddenincorrect" name="incorrect[]" value="<?php echo$inc_ans[$row_count];?>">
    </td>

    <td>
    <input  name="answerMarks[]" type="text" data-type="qmark" value='0'  />
    </td>

    </tr>
        <?php
            //remaining incorrect answers in separate row (if any) follows here
        if($row_count < $q_row_span - 1) 
        {
            for($i=($row_count + 1); $i<$q_row_span; $i++) { ?>     
                <tr>
                <td>
                <input type="hidden" class="hiddenincorrect" name="incorrect[]" value="<?php echo$inc_ans[$i];?>">
                </td>

                <td class="answermarkstd">
                <input  name="answerMarks[]" type="text" data-type="qmark" value='0'  />            
                </td>
                </tr>
        <?php
            }
        }
    }

    ?>
    </table>
<input id="submitBtn" name="submitPenalty" type="submit" value="Submit Marks" />
    </form>

下面是jquery处理提交:

myClickHandler = function(e) {

       if (confirm("Are you sure you want to Proceed?" + "\n")) {
         $.ajax({
           url: "insertpenaltymarks.php",
           data: $("#PenaltyMarks").serialize(),
           async: false,
           type: "POST"
         });
         return true;
      } else {
        return false;
      }
    };

    $('#PenaltyMarks').submit(myClickHandler);

});

更新:

Question Table:

QuestionId (PK auto)  (int 10)
QuestionNo  (int 4)
QuestionContent (varchar 5000)


Penalty_Marks Table;

PenaltyAnswerId (PK auto) (int 10)
PenaltyAnswer (Varchar 5)
PenaltyMarks (int 3)
QuestionId (FK)  (int 10) Reference to QuestionId in Question Table 
4

1 回答 1

2

更新

使用您当前的代码,您可以var_dump发现$_POST['numQuestion']数组包含的成员可能比$_POST['incorrect'][$i]数组少。我也更新了我的代码。

<form id="PenaltyMarks" action="insertpenaltymarks.php" method="post">

<table id='penaltytbl'>
<?php

foreach($ques_ans as $questionId => $inc_ans)
{
    $q_row_span = count($inc_ans);
    $row_count = 0;
    $inc_ans = array_values($inc_ans);
?>

<tr class="questiontd">
<td>
<input type="hidden" name="numQuestion[<?php echo $questionId; ?>]" value="<?php echo$questionId?>" />
</td>

<td>
<input type="hidden" class="hiddenincorrect" name="incorrect[<?php echo $questionId; ?>][]" value="<?php echo$inc_ans[$row_count];?>">
</td>

<td>
<input  name="answerMarks[<?php echo $questionId; ?>][]" type="text" data-type="qmark" value='0'  />
</td>

</tr>
    <?php
        //remaining incorrect answers in separate row (if any) follows here
    if($row_count < $q_row_span - 1) 
    {
        for($i=($row_count + 1); $i<$q_row_span; $i++) { ?>     
            <tr>
            <td>
            <input type="hidden" class="hiddenincorrect" name="incorrect[<?php echo $questionId; ?>][]" value="<?php echo $inc_ans[$i];?>">
            </td>

            <td class="answermarkstd">
            <input  name="answerMarks[<?php echo $questionId; ?>][]" type="text" data-type="qmark" value='0' />            
            </td>
            </tr>
    <?php
        }
    }
}

?>
</table>
<input id="submitBtn" name="submitPenalty" type="submit" value="Submit Marks" />
</form>

插入脚本也应该更改:)

foreach($_POST['incorrect'] as $questionId => $incorrectArray)
{
    for($i = 0; $i < count($incorrectArray), $i++)
    {
        $incorrect = $_POST['incorrect'][$questionId][$i];
        $answerMarks = $_POST['answerMarks'][$questionId][$i];
        $numQuestion = $_POST['numQuestion'][$questionId];

        $insertanswer->bind_param('sii', $incorrect, $answerMarks, $numQuestion);
        $insertanswer->execute();
    }
}
于 2013-01-18T11:52:06.127 回答