2013-01-03 54 views
0

我想執行插入到數據庫中,使得在數據庫表Individual Answer包含以下數據:是什麼導致數據不能被插入到數據庫表中?

Individual_Answer表:

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

問題是,它不插入到數據庫中。它是導致問題的PHP/mysqli代碼,還是它沒有訪問正在執行插入的PHP頁面(insertmarks.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> 

回答

3

您沒有execute查詢,

$insertanswer->execute(); 
+0

我可以很快問我把代碼放在哪裏,因爲我把這行代碼放在close()語句的正上方,但它只插入一行'AnswerId:299 AnswerMarks:3',它沒有插入其他值 – user1881090

+0

@ user1881090將其放在bind語句之後。 – Musa

相關問題