Search code examples
phpajaxmultiple-insert

Multiple insert in MySQL with Ajax


Hey guys, I'm having trouble in inserting my values into MySQL using PHP and Ajax. I'm trying to insert into MySQL with single button with multiple input.

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

                        }

}

?>

But there's an error when I checked on firebug says "You have an error in your SQL syntax;"

I can't find what I've done wrong.. Please help, guys, because I'm just a newbie in Ajax and PHP.


Solution

  • It looks like you start storing the SQL in the variable $save_grades_boy and then try concatenating the values to $ready_save_grades_boy which doesn't exist yet and therefore is treated as an empty string.

    Replace

    $ready_save_grades_boy .= $values_boy;
    

    With

    $save_grades_boy .= $values_boy;
    

    Or

    $ready_save_grades_boy = $save_grades_boy . $values_boy;