mysqli_query($ connect,$ query)返回false:如何解决?

时间:2019-06-07 15:54:09

标签: php html mysql apm

由于正在分配大学,我突然使用了我没学过的APM。

在cmd上测试时,该查询运行良好,但mysqli_query不起作用。我不知道问题出在哪里,所以我现在很困。

echo $query

输出为:

  

mysqli_error(INSERT INTO教授(p_name,l_num,p_num)VALUES('rho',3008,2002); INSERT INTO讲座(l_num,l_name,week,start_time,end_time)VALUES(3008,'database','monwed ','14:30:00','16:00:00');更新讲座SET p_id =(从教授那里选择MAX(p_id))WHERE l_num = 3008;更新讲座SET r_id =(从房间里r_id选择SELECT r_id ='s111')WHERE l_num = 3008;)

这是我的代码。

<?php

    include './dbcon.php';

    $lnum = (int) $_POST['lnum'];         //학수번호
    $lname = $_POST['lname'];    //강의명
    $pname = $_POST['pname'];         //교수명
    $pnum = (int) $_POST['pnum'];         //교수번호
    $rname = $_POST['rname'];         //강의실

    $startt = $_POST['start_t'];
    $endt = $_POST['end_t'];

    $day = "";       //요일

    if(!empty($_POST['weekdays'])) {
        foreach($_POST['weekdays'] as $selected) {

          $check_sql=mysqli_query($connect,"SELECT * from room
                  join lecture on room.r_id=lecture.r_id
                  join building on room.b_num=building.b_num
                  where r_name='$rname'
                  and (week like '%$selected%'
                  and (('$startt' < start_time and start_time < '$endt')
                  or (start_time <='$startt' and '$endt'<=end_time)
                  or ('$startt' < end_time and end_time < '$endt')));");

          $check = mysqli_num_rows($check_sql);

          if($check > 0){
            echo "<script>
                  alert('You cannout add lecture.');
                  location.href='./admin.php';
                  </script>" . mysqli_error($check);
          }
          $day .= $selected;
        }
    }

    $query="INSERT INTO professor(p_name,l_num,p_num) VALUES('$pname',$lnum,$pnum);
            INSERT INTO lecture(l_num,l_name,week,start_time,end_time) VALUES($lnum,'$lname','$day','$startt','$endt');
            UPDATE lecture SET p_id= (SELECT MAX(p_id) FROM professor) WHERE l_num=$lnum;
            UPDATE lecture SET r_id = (SELECT r_id FROM room WHERE r_name='$rname') WHERE l_num=$lnum;";

    if (mysqli_query($connect, $query)) {
        echo "<script>alert('Success insert');location.href='./admin.php';</script>";
    } else {
        //echo "mysqli_error($query)";
        echo "<script>alert('Fail to insert');location.href='./admin.php';</script>" . mysqli_error($query);
    }
 ?>

1 个答案:

答案 0 :(得分:-1)

mysqli_query()一次只能执行一个查询。

您可以将以下查询分成多个查询,然后依次执行

$query="INSERT INTO professor(p_name,l_num,p_num) VALUES('$pname',$lnum,$pnum);
        INSERT INTO lecture(l_num,l_name,week,start_time,end_time) VALUES($lnum,'$lname','$day','$startt','$endt');
        UPDATE lecture SET p_id= (SELECT MAX(p_id) FROM professor) WHERE l_num=$lnum;
        UPDATE lecture SET r_id = (SELECT r_id FROM room WHERE r_name='$rname') WHERE l_num=$lnum;";

或者改用mysqli_multi_query()


注意:我强烈建议您使用许多prepared statement而不是1 mysqli_multi_query()。您的查询实际上向SQL Injections

开放