0

我想在数据库中执行插入操作,以便数据库表Individual Answer包含以下数据:

Individual_Answer 表:

AnswerId  AnswerMarks
295       2
296       1
297       1
298       3
299       3

问题是它没有插入数据库。是导致问题的php / mysqli代码还是没有访问正在执行插入(insertmarks.php)的php页面的ajax?

下面是我在 php/mysqli 中的代码:

<?php

 // connect to the database
 include('connect.php');

  /* check connection */
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    die();
  }

var_dump($_POST);  

 $answersql = "INSERT INTO Individual_Answer (AnswerId, AnswerMarks) 
    VALUES (?, ?)";

if (!$insertanswer = $mysqli->prepare($answersql)) {
    // Handle errors with prepare operation here
    echo __LINE__.': '.$mysqli->error;
}



//make sure both prepared statements succeeded before proceeding
if($insertanswer)
{
   $c = count($_POST['answerId']);

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


$insertanswer->bind_param('ii', $_POST['answersId'][$i], $_POST['answerMarks'][$i]);

}
    //close your statements at the end

    $insertanswer->close();
}

?>

下面是var_dump($_POST)输出:

array(7) { 
["q1_ans_org"]=> string(1) "4" 
["q1_ans"]=> string(1) "0" 
["answerMarks"]=> array(5) 
{ 
[0]=> string(1) "2" 
[1]=> string(1) "1" 
[2]=> string(1) "1" 
[3]=> string(1) "3" 
[4]=> string(1) "3" } 
["q2_ans_org"]=> string(1) "6" 
["q2_ans"]=> string(1) "0" 
["num_groups"]=> string(1) "2" 
["submitMarks"]=> string(12) "Submit Marks" 
} 

下面是假设访问 insertmarks.php 的 html 和 jquery/ajax 代码:

<form id="Marks" action="penalty.php" method="post">

...

<p>
<input type='hidden' id='num_groups' name='num_groups' value='<?php echo$questionId?>'>
<input id="submitBtn" name="submitMarks" type="submit" value="Submit Marks" />
</p>

</form>

...

<script type="text/javascript">

myClickHandler = function(e) {
       var ng = $('#num_groups').val();
       for (var group = 1; group <= ng; group++) {
         if (!validation(group)) return false;
       }
       if (confirm("Make sure that your marks are correct, once you proceed after this stage you would not be able to go back and change any of your marks for this Assessment." + "\n" + "\n" + "Are you sure you want to Proceed?" + "\n")) {
         $.ajax({
           url: "insertmarks.php",
           data: $("#Marks").serialize(),
           async: false,
           type: "POST"
         });
         return true;
      } else {
        return false;
      }
    };

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

});

</script>
4

1 回答 1

3

你没有执行查询,

$insertanswer->execute();
于 2013-01-03T03:16:41.937 回答