PHP多输入搜索查询mysql数据库

时间:2016-02-23 13:14:16

标签: php mysql

我正在尝试使用具有多个输入搜索的表单来查询Mysql,但由于某种原因,最终没有任何结果。 我正在使用我在堆栈中找到的一些代码 - PHP Multiple input search

这是我的html表单

        <form id="SearchFlightForm" method="post" action="searchresult.php" accept-charset='UTF-8'>

    <div class="col-md-12 product-search-title">Search For Flights Deals </div>
    <div class="col-md-12 search-col-padding">
    <label class="radio-inline">
    <input type="radio" name="inlineradiooptions" id="inlineRadio1" value="One Way"> One Way</label>
    <label class="radio-inline">
    <input type="radio" name="inlineradiooptions" id="inlineRadio2" value="Round Trip"> Round Trip</label>
    </div>
    <div class="clearfix"></div>
    <div class="col-md-6 col-sm-6 search-col-padding">
    <label>Leaving From</label>
    <div class="input-group">
    <input type="text" name="departure_city" class="form-control" required placeholder="E.g. London">
    <span class="input-group-addon"><i class="fa fa-map-marker fa-fw"></i></span></div></div>
    <div class="col-md-6 col-sm-6 search-col-padding">
    <label>Leaving To</label>
    <div class="input-group">
    <input type="text" name="destination_city" class="form-control" required placeholder="E.g. New York">
    <span class="input-group-addon"><i class="fa fa-map-marker fa-fw"></i></span></div></div>
    <div class="clearfix"></div>
    <div class="col-md-6 col-sm-6 search-col-padding">
    <label>Departure</label>
    <div class="input-group">
    <input type="text" id="departure_date" name="departure_date" class="form-control" placeholder="DD/MM/YYYY">
    <span class="input-group-addon"><i class="fa fa-calendar fa-fw"></i></span>
    </div>
    </div>
    <div class="col-md-6 col-sm-6 search-col-padding">
    <label>Return</label>
    <div class="input-group">
    <input type="text" id="return_date" class="form-control" name="return_date" placeholder="DD/MM/YYYY">
    <span class="input-group-addon"><i class="fa fa-calendar fa-fw"></i></span>
    </div>
    </div>
    <div class="clearfix"></div>
    <div class="col-md-3 col-sm-3 search-col-padding">
    <label>Adult</label><br>
    <input id="adult_count" name="adult_count" value="1" class="form-control quantity-padding">
    </div>
    <div class="col-md-3 col-sm-3 search-col-padding">
    <label>Child</label><br>
    <input type="text" id="child_count" name="child_count" value="0" class="form-control quantity-padding">
    </div>
    <div class="col-md-3 col-sm-3 search-col-padding">
    <label>Infants</label><br>
    <select class="selectpicker" name="infants">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    </select>
    </div>
<div class="col-md-3 col-sm-3 search-col-padding">
<label>Class</label><br>
<select class="selectpicker" name="flightclass">
<option value="economy">Economy</option>
<option value="business">Business</option>
</select>
</div>
<div class="clearfix"></div>
<div class="col-md-12 search-col-padding">
<button type="submit" name="submit" id="submit" class="search-button btn transition-effect">Search Flights</button>
</div>
<div class="clearfix"></div>
</form>

此处为操作文件

    <?php
// Process the search query

include('ligacaoBD.php');


  if(isset($_POST['submit'])) {
    // define the list of fields
     $fields = array('id', 'inlineradiooptions', 'departure_city', 'destination_city', 
     'departure_date', 'return_date', 'adult_count', 'child_count', 'infants', 'flightclass');
    $conditions = array();
   }


// builds the query
$query = "SELECT id, airline_image, flight_class, departure_date, departure_time, departure_location, arrival_date, arrival_time, arrival_location, flight_stops, flight_duration FROM flight_details WHERE id=1";
// if there are conditions defined
if(count($conditions) > 0) {
    // append the conditions
    $query .= " WHERE " . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
}



   // loop through the defined fields
foreach($fields as $field){
    // if the field is set and not empty
    if(isset($_POST[$field]) && $_POST[$field] != '') {
        // create a new condition while escaping the value inputed by the user (SQL Injection)
        $conditions[] = "'$field' LIKE '%" . mysqli_real_escape_string($mysqli_link, $_POST[$field]) . "%'";
    }
}


// if there are conditions defined
if(count($conditions) > 0) {
    // append the conditions
    $query .= " WHERE " . implode (' AND ', $conditions); // you can change to 'OR', but I suggest to apply the filters cumulative
}


$result = mysqli_query($mysqli_link, $query);



mysqli_close($mysqli_link);

    if(isset($_POST['submit'])) {
        while($row = mysqli_fetch_array($result)) {
        $id = $row['id'];
        $flight_image = $row['flight_image'];
        $flight_class = $row['flight_class'];
        $departure_date = $row['departure_date'];
        $departure_time = $row['departure_time'];
        $departure_location = $row['departure_location'];
        $arrival_date = $row['arrival_date'];
        $arrival_time = $row['arrival_time'];
        $arrival_location = $row['arrival_location'];
        $flight_stops = $row['flight_stops'];
        $flight_duration = $row['flight_duration'];

       echo "<b>id: $id</b><br> image: $flight_image<br>Class: $flightclass<br>departure_date: $departure_date<br>departure_time: $departure_time<br>departure_location: $departure_location<br>arrival_date: $arrival_date<br>arrival_time: $arrival_time<br>arrival_location: $arrival_location<br>flight_stops: $flight_stops<br>flight_duration: $flight_duration<hr><br>";
        }
    }   
?>

任何人都可以暗示我做错了什么。

感谢S.C

0 个答案:

没有答案