PHP数组为空,没有数据显示

时间:2018-04-03 23:41:13

标签: php html

我试图通过查询获取搜索结果,但由于某种原因,它返回空结果。 sql语句有效,在phpMyadmin的终端上试了一下。所以我能找到的唯一错误就是php <?php session_start(); ?> <!DOCTYPE html> <html lang="en"> <?php require_once "../controller/airportList.php" ?> <?php require_once "../controller/flightList.php" ?> <head> </head> <body> <div class="panel-body"> <form action = "../controller/flightList.php" method="post"> <div class="form-group"> <label for="sel1">From</label> <select class="form-control" id="indexDeparture" name="indexDeparture"> <option></option> <?php foreach ($airportList as $airport): ?> <option value="<?= $airport->IATA_Code ?>" ><?= $airport->Airport_Location ?> (<?= $airport->IATA_Code ?>)</option> <?php endforeach ?> </select> </div> <div class="form-group"> <label for="sel2">To</label> <select class="form-control" id="indexDestination" name ="indexDestination"> <option></option> <?php foreach ($airportList as $airport): ?> <option value="<?= $airport->IATA_Code ?>"><?= $airport->Airport_Location ?> (<?= $airport->IATA_Code ?>)</option> <?php endforeach ?> </select> </div> <div class="form-group"> <label class="control-label" for="date"><br/>Departure Date</label> <input class="form-control" id="indexDate" name="indexDate" placeholder="YYYY-MM-DD" type="date"/> </div> <div class="form-group"> <!-- Submit button --> <button class="btn btn-primary " type="submit">Search</button> </div> </form> </div> </div> </div> </div> </body> </html> 没有从视图页面获取任何值。我将搜索查询输出到另一个页面,并将其显示为html表格。

以下页面是视图页面:

 <?php
    require_once "../model/Flight.php";
    require_once "../model/Database_Access.php";


    if ( !empty($_REQUEST['indexDeparture'])  &&  !empty($_REQUEST['indexDestination'])
     &&  !empty($_REQUEST['indexDate']) ){

    //$flightsearch = new Flight();
      $departure = htmlentities($_REQUEST['indexDeparture']);
      $destination = htmlentities($_REQUEST['indexDestination']);
      $date = htmlentities($_REQUEST['indexDate']);
      $flightList = Database_Access::getInstance()->getFlightsByAll($departure, $destination, $date);
      header("Location: ../view/searchResults.php");
    }
    ?>

以下页面是上面视图页面的php控制器:  问题似乎出现在控制器中。

  function getFlightsByAll($departure, $destination, $date){
    $statement = $this->pdo->prepare("SELECT * FROM Flights WHERE Departure_IATA_Code = ? AND Arrival_IATA_Code = ? AND Departure_Date = ?");
    $statement->execute([$departure,
                         $destination,
                         $date]);
    $results = $statement->fetchAll(PDO::FETCH_CLASS,"Flight");
    return $results;
  }

这是从数据库中获取数据的模型代码:

  <div class="panel panel-default">
  <div class="tab-pane">
      <h3>Flights</h3>
      <?php if( !empty($flightList)): ?>
      <table class="table table-bordered">
        <thead>
        <th>Flight Number</th>
        <th>Aircraft ID</th>
        <th>Departure IATA Code</th>
        <th>Departure Date</th>
        <th>Arrival IATA Code</th>
        <th>Cost</th>
        <th>Departure Time</th>
        <th>Flight Duration</th>
        </thead>
       <?php foreach ($flightList as $flight): ?>
        <tr>
          <td><?=$flight->Flight_Number?></td>
          <td><?=$flight->Aircraft_ID?></td>
          <td><?=$flight->Departure_IATA_Code?></td>
          <td><?=$flight->Departure_Date?></td>
          <td><?=$flight->Arrival_IATA_Code?></td>
          <td><?=$flight->Cost?></td>
          <td><?=$flight->Departure_Time?></td>
          <td><?=$flight->Flight_Duration?></td>
        </tr>
      <?php endforeach ?>
    </table>
  <?php endif ?>
  </div>
  </div>

这是searchResults页面:

{{1}}

1 个答案:

答案 0 :(得分:-1)

    if (!empty($_POST['indexDeparture']) && !empty($_POST['indexDestination']) && !empty($_POST['indexDate'])){
     // all input fields are NOT empty... do stuff with this

   //$flightsearch = new Flight();
      $departure = htmlentities($_POST['indexDeparture']);
      $destination = htmlentities($_POST['indexDestination']);
      $date = htmlentities($_POST['indexDate']);

      $flightList = Database_Access::getInstance()->getFlightsByAll($departure, $destination, $date);
      header("Location: ../view/searchResults.php");
    }

您正在从表单发布到此控制器,因此使用$ _POST可以解决您的问题。