我只收到未定义变量的警告

pexxcrt2  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(325)

这个问题在这里已经有答案了

使用php的“notice:未定义变量”、“notice:未定义索引”和“notice:未定义偏移量”(28个答案)
两年前关门了。
我创建了这个php服务来从数据库中检索数据并显示tblèu的结果,但我只收到警告。请看下面的代码。

<?php

//fetch_data.php

$connect = new PDO("mysql:host=localhost;dbname=kawadb", "root", "");

$method = $_SERVER['REQUEST_METHOD'];

    if($method == 'GET'
      //&& isset($_GET['hall_id']) && isset($_GET['hall_name']) && isset($_GET['hall_department']) && isset($_GET['hall_floor']) && isset($_GET['capacity']) 

      )
    {

       $data = array(
        ':hall_name'   => "%" . $_GET['hall_name'] . "%",
        ':hall_department'     => "%" . $_GET['hall_department'] . "%",
        ':hall_floor'    => "%" . $_GET['hall_floor'] . "%",
        ':capacity'    => "%" . $_GET['capacity'] . "%"
       );

{

       $query = "SELECT * FROM tbl_halls WHERE hall_name LIKE :hall_name AND hall_department LIKE :hall_department AND hall_floor LIKE :hall_floor AND capacity LIKE :capacity BY hall_id DESC";

       $statement = $connect->prepare($query);
       $statement->execute($data);
       $result = $statement->fetchAll();
       foreach($result as $row)
       {
          $output[] = array( 
           'hall_id'    => $row['hall_id'],  
           'hall_name'   => $row['hall_name'],
           'hall_department'    => $row['hall_department'],
           'hall_floor'   => $row['hall_floor'],
           'capacity'   => $row['capacity']
          );
       }
       header("Content-Type: application/json");
       echo json_encode($output);
    }

if($method == "POST")
{
 $data = array(
  ':hall_name'  => $_POST["hall_name"],
  ':hall_department'    => $_POST["hall_department"],
  ':hall_floor'   => $_POST["hall_floor"],
  ':capacity'   => $_POST["capacity"]
 );

 $query = "INSERT INTO tbl_halls (hall_name, hall_department, hall_floor,capacity) VALUES (:hall_name, :hall_department, :hall_floor, :capacity)";
 $statement = $connect->prepare($query);
 $statement->execute($data);
}

if($method == 'PUT')
{
 parse_str(file_get_contents("php://input"), $_PUT);
 $data = array(
  ':hall_id' => $_PUT['hall_id'],
  ':hall_name' => $_PUT['hall_name'],
  ':hall_department'   => $_PUT['hall_department'],
  ':hall_floor'  => $_PUT['hall_floor'],
  ':capacity'  => $_PUT['capacity']
 );
 $query = "
 UPDATE tbl_halls 
 SET hall_name = :hall_name, 
 hall_department = :hall_department, 
 hall_floor = :hall_floor,
 capacity = :capacity,
 WHERE hall_id = :hall_id
 ";
 $statement = $connect->prepare($query);
 $statement->execute($data);

}

if($method == "DELETE")
{
 parse_str(file_get_contents("php://input"), $_DELETE);
 $query = "DELETE FROM tbl_halls WHERE hall_id = '".$_DELETE["hall_id"]."'";
 $statement = $connect->prepare($query);
 $statement->execute();
}

}
?>

谨致问候
我试过了

if($method == 'GET' &&
   isset($_GET['hall_id']) && isset($_GET['hall_name']) &&
   isset($_GET['hall_department']) && isset($_GET['hall_floor']) && 
   isset($_GET['capacity'])

但我什么也没得到
我尝试过显示表中的数据,但每个字段的输出都是未定义的索引:hall\u name。

bbmckpt7

bbmckpt71#

在我看来,你有一个额外的花括号或缺少一个条件语句

if($method == 'GET' ){
  $data = array(
  ':hall_name'   => "%" . $_GET['hall_name'] . "%",
  ':hall_department'     => "%" . $_GET['hall_department'] . "%",
  ':hall_floor'    => "%" . $_GET['hall_floor'] . "%",
  ':capacity'    => "%" . $_GET['capacity'] . "%"
  );
);

**{**

$query

也应该是 $output = array(...)$output[] = array(...) 可能还有一些语法问题,但我马上就注意到了。

相关问题