0

I'm trying to query Mysql with a form with multiple input search but for some reason, in the end, nothing come out. I'm using some code that I found here on stack - PHP Multiple input search

Here my html form

        <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>

Here the action file

    <?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>";
        }
    }   
?>

Please could anyone give a hint of what I'm doing wrong.

thanks S.C

7
  • no idea if if(isset($_POST['submit'])) is even firing up, since you cut off the rest of your form. check for errors via PHP and MySQL. It's all I can say here. Plus, you may be closing your connection too soon. Commented Feb 23, 2016 at 13:20
  • You're closing the database connection before fetching from it. Commented Feb 23, 2016 at 13:21
  • @JayBlanchard I just caught that too and made an edit before seeing your comment there Sam. GMTA Sam ;-) Commented Feb 23, 2016 at 13:22
  • 1
    Eagle-eye Ralph! @Fred-ii- Commented Feb 23, 2016 at 13:22
  • Plus, you probably need to remove that conditional if(isset($_POST['submit'])) { for the result set. Commented Feb 23, 2016 at 13:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.