皆さん、PHP と Ajax を使用して MySQL に値を挿入するのに問題があります。複数の入力を備えた単一のボタンでMySQLに挿入しようとしています。
JS:
$('#save_grade_button').click(function (){
var B_grade = [];
var G_grade = [];
$.each($("input[id^='student_grde_B']"), function(i, item) {
var grade_B = $(item).val();
var stud_id_B = $(item).attr('rel');
B_grade.push({"studnt_B_id":stud_id_B,"studnt_grade_B":grade_B});
});
$.each($("input[id^='student_grde_G']"), function(i, item) {
var grade_G = $(item).val();
var stud_id_G = $(item).attr('rel');
G_grade.push({"studnt_G_id":stud_id_G,"studnt_grade_G":grade_G});
});
subjct_id = $('#subjects-list').val();
year_grade_level = $('#year_grade_lvl').val();
sbjct_handler_id = $('#assign-handler-id').val();
$.ajax({
type:'POST',
url:'grades.php',
dataType:'json',
data:{'swtch_numbr':'1','subject_id':subjct_id,'year_level':year_grade_level,'subject_handler_id':sbjct_handler_id,'student_grades_boy':B_grade,'student_grades_girl':G_grade},
success:function (data){
}
});
});
PHP:
<?php
include_once('DBconnect.php');
$switch_num = $_POST['swtch_numbr'];
switch ($switch_num) {
case 1:
save_grades();
break;
}
function save_grades(){
session_start();
$school_id = $_SESSION['school_id'];
$faculty_id = $_SESSION['user_id_fac'];
$subject_id = $_POST['subject_id'];
$year_grade_level = $_POST['year_level'];
$subject_handeler_id = $_POST['subject_handler_id'];
$student_grades_boy = $_POST['student_grades_boy'];
$student_grades_girl = $_POST['student_grades_girl'];
$save_grades_boy = "INSERT INTO registrar_grade_archive
(registrar_archive_id,
school_id,
subject_id,
grade,
advisory_faculty_id,
subject_handler_id,
year_level,
student_id) VALUES";
$values_boy = array();
$values_girl = array();
foreach ($student_grades_boy as $key=>$data) {
$student_id_B= $data['studnt_B_id'];
$grade_B = $data['studnt_grade_B'];
$values_boy[$key] = '(\''.$school_id.'\', \''.$subject_id.'\',\''.$grade_B.'\',\''.$faculty_id.'\',\''.$subject_handeler_id.'\',\''.$year_grade_level.'\',\''.$student_id_B.'\')';
}
$values_boy = implode(', ', $values_boy);
$ready_save_grades_boy .= $values_boy;
if(@!mysql_query($ready_save_grades_boy)){
die('error insert'.mysql_error());
}
}
?>
しかし、firebug をチェックすると、「SQL 構文にエラーがあります」というエラーが表示されます。
私が間違ったことを見つけることができません..私はAjaxとPHPの初心者なので、助けてください。