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

时间:2018-12-21 22:06:56

标签: php html mysql

我已经创建了该php服务,用于从数据库中检索数据并显示tbl_halls的结果,但我仅收到警告。请查看下面的代码。

<?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_name。

1 个答案:

答案 0 :(得分:0)

在我看来,您好像有一个大括号或缺少条件语句

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(...)

可能还有更多的语法问题,但那些问题立即引起了我的注意。