尝试运行multiqyery时出错

时间:2018-05-24 09:23:16

标签: php mysql mysqli

对不起,如果它有点小错误。我正在使用第一次多查询。错误1和错误2两个代码相同,只是在错误2插入行中添加了单个冒号('')。如果我从for循环内部回显显示一切正常。一段时间成功但没有插入数据库。提前谢谢。

  

错误1:错误:您的SQL语法出错;检查手册   对应于您的MySQL服务器版本,以获得正确的语法   使用'INSERT INTO student_attendance(attendance_date,   出勤率_class_id,出勤率在第1行

 isAnswerProvided(event: any, check:any)
  {
    console.log("question answer not provided responseId:: ",this.responseId, " questionId::",this.questionId, "  check::", check );
    console.log(event);
  }
  

错误2:SQL语法中有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   ''attendance_date','attendance_class_id'附近,   'attendance_section_id','出勤率'在第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));

1 个答案:

答案 0 :(得分:0)

有两个错误。首先,在您的for循环中,您必须在每个INSERT字符串后添加;。其次,如果你想使用多个插入,你应该在循环之前编写INSERT部分字符串,然后只添加带有值的行,使用,分隔符。

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

OR

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 = 'INSERT INTO student_attendance (attendance_date, attendance_class_id, attendance_section_id, attendance_student_id, attendance_present_absent ) VALUES ';

     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 .= ($count>0?",":"") . "('{$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>";

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