Home » Php » Error trying to run a multiqyery

Error trying to run a multiqyery

Posted by: admin June 30, 2018 Leave a comment

Questions:

Sorry if its a little mistake. i was using first time multi query. error 1 and error 2 both codes are same except single colon(”) added in error 2 insert row. if i echo from inside the the for loop shows everything fine. some time success but not inserted in data base. Thank You in advance.

Error 1: Error :You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near ‘INSERT INTO student_attendance (attendance_date,
attendance_class_id, attendance’ at line 1


include("../includes/db.php");

if(!empty($_POST)) {

  $student_attendance_id            =   $_POST['student_attendance_id'];
  $attendance_date                  =   $_POST['attendance_date'];
  $attendance_class_id              =   $_POST['attendance_class_id'];
  $attendance_section_id            =   $_POST['attendance_section_id'];
  $attendance_student_id            =   $_POST['attendance_student_id'];
  if(isset($_POST['attendance_present_absent'])){
    $attendance_present_absent = $_POST['attendance_present_absent'];
  } else {
    $attendance_present_absent        =  '';
  }

$query = '';

         for($count = 0; $count<count($attendance_student_id); $count++)
    {

      $attendance_date_now              = mysqli_real_escape_string($connection, $attendance_date);
      $student_attendance_id_now        = mysqli_real_escape_string($connection, $student_attendance_id[$count]);
      $attendance_class_id_now          = mysqli_real_escape_string($connection, $attendance_class_id[$count]);
      $attendance_section_id_now        = mysqli_real_escape_string($connection, $attendance_section_id[$count]);
      $attendance_student_id_now        = mysqli_real_escape_string($connection, $attendance_student_id[$count]);
      $attendance_present_absent_now    = mysqli_real_escape_string($connection, $attendance_present_absent[$count]);

      $query .= "INSERT INTO student_attendance (attendance_date, attendance_class_id, attendance_section_id, attendance_student_id, attendance_present_absent ) ";
      $query .= "VALUES ('{$attendance_date_now}', '{$attendance_class_id_now}', '{$attendance_section_id_now}', '{$attendance_student_id_now}', '{$attendance_present_absent_now}' ) ";

      echo $attendance_date_now;
      echo $attendance_class_id_now . $attendance_section_id_now . "<br>";

    }
$result = mysqli_multi_query($connection, $query) or die("Error :" . mysqli_error($connection));

Error 2:You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near ”attendance_date’, ‘attendance_class_id’,
‘attendance_section_id’, ‘attendance_s’ at line 1


include("../includes/db.php");

if(!empty($_POST)) {

  $student_attendance_id            =   $_POST['student_attendance_id'];
  $attendance_date                  =   $_POST['attendance_date'];
  $attendance_class_id              =   $_POST['attendance_class_id'];
  $attendance_section_id            =   $_POST['attendance_section_id'];
  $attendance_student_id            =   $_POST['attendance_student_id'];
  if(isset($_POST['attendance_present_absent'])){
    $attendance_present_absent = $_POST['attendance_present_absent'];
  } else {
    $attendance_present_absent        =  '';
  }

  for($count = 0; $count<count($attendance_student_id); $count++)
{

  $attendance_date_now              = mysqli_real_escape_string($connection, $attendance_date);
  $student_attendance_id_now        = mysqli_real_escape_string($connection, $student_attendance_id[$count]);
  $attendance_class_id_now          = mysqli_real_escape_string($connection, $attendance_class_id[$count]);
  $attendance_section_id_now        = mysqli_real_escape_string($connection, $attendance_section_id[$count]);
  $attendance_student_id_now        = mysqli_real_escape_string($connection, $attendance_student_id[$count]);
  $attendance_present_absent_now    = mysqli_real_escape_string($connection, $attendance_present_absent[$count]);

  $query .= "INSERT INTO student_attendance ('attendance_date', 'attendance_class_id', 'attendance_section_id', 'attendance_student_id', 'attendance_present_absent' ) ";
  $query .= "VALUES ('{$attendance_date_now}', '{$attendance_class_id_now}', '{$attendance_section_id_now}', '{$attendance_student_id_now}', '{$attendance_present_absent_now}' ) ";

  echo $attendance_date_now;
  echo $attendance_class_id_now . $attendance_section_id_now . "<br>";

}

$result = mysqli_multi_query($connection, $query) or die("Error :" . mysqli_error($connection));
Answers: