doubingjiu3199 2013-01-18 11:48
浏览 45
已采纳

通过循环时数据没有正确惰化

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

}

The above insert is not inserting all the incorrect answers into the table, it is only inserting one incorrect answer per question. What am I doing wrong?

Below is the currect db table looks like:

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

Below is what it should of been like:

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

Below is form:

<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>

Below is jquery handling submit:

myClickHandler = function(e) {

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

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

});

UPDATE:

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 
  • 写回答

1条回答 默认 最新

  • doudong2149 2013-01-18 11:52
    关注

    Updated

    With your current code, you can var_dump and found that the $_POST['numQuestion'] array may contains a fewer member than the $_POST['incorrect'][$i] array. I have updated my code too.

    <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>
    

    The insertion script should be changed too :)

    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();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行