0

嘿伙计们,我在使用 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());

                        }

}

?>

但是当我检查萤火虫时出现错误说“您的 SQL 语法有错误;”

我找不到我做错了什么。请帮助,伙计们,因为我只是 Ajax 和 PHP 的新手。

4

1 回答 1

1

看起来您开始将 SQL 存储在变量 $save_grades_boy 中,然后尝试将这些值连接到 $ready_save_grades_boy 尚不存在,因此被视为空字符串。

代替

$ready_save_grades_boy .= $values_boy;

$save_grades_boy .= $values_boy;

或者

$ready_save_grades_boy = $save_grades_boy . $values_boy;
于 2013-01-11T17:56:30.457 回答