尝试运行multiqyery时出错

os8fio9y  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(461)

抱歉,如果这是一个小错误。我用的是第一次多重查询。错误1和错误2除了在错误2插入行中添加了一个冒号(“”)之外,这两个代码都是相同的。如果我从for循环内部回音,就会显示一切正常。一段时间成功但没有插入数据库。先谢谢你。
错误1:错误:您的sql语法有错误;请查看与您的mysql服务器版本对应的手册,以便在第1行的“insert into student\u attention(attention\u date,attention\u class\u id,attention)”附近使用正确的语法

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

错误2:您的sql语法有错误;检查与mysql服务器版本相对应的手册,在第1行的“出勤日期”、“出勤班级id”、“出勤部门id”、“出勤s”附近使用正确的语法

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

egmofgnx1#

有两个错误。首先,在for循环中,必须添加 ; 在每个插入字符串之后。第二,如果您想使用多个insert,您应该在for cycle之前编写insert part string,然后只添加带有值的行,这是有必要的 , 使用了分隔符。

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

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

相关问题