Display search result by joining tables in php
Good day everyone
I needed users to search for cars in my application by brand and location.
I created two drop down fields “Brand” and “Location” that is populated from database.
When users choose the prefered brand and location, and search button is clicked.
It will open a new page displaying the related search.
The challenges i am having right now is how to join the tables in order to display the
required result to the users.
See below code
Page1.php
<div class="form-group">
<label for="exampleSelectGender">Select City/Location<span
style="color:red">*</span></label>
<select class="form-control" name="location" required id="exampleSelectGender">
<option>---Select Location---</option>
<?php $ret = "select LocationName,LocationName from tbllocations";
$query = $dbh->prepare($ret);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
if ($query->rowCount() > 0) {
foreach ($results as $result) {
?>
<option value="<?php echo htmlentities($result->LocationName); ?>">
<?php echo htmlentities($result->LocationName); ?></option>
<?php }}?>
</select>
</div>
<div class="form-group">
<label for="exampleSelectGender">Select Brand<span
style="color:red">*</span></label>
<select class="form-control" name="brand" required id="exampleSelectGender">
<option>---Select Brand---</option>
<?php $sql = "SELECT * from tblbrands ";
$query = $dbh->prepare($sql);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
$cnt = 1;
if ($query->rowCount() > 0) {
foreach ($results as $result) {?>
<option value="<?php echo htmlentities($result->brandID); ?>">
<?php echo htmlentities($result->BrandName); ?></option>
<?php }}?>
</select>
</div>
<div class="spacer-10"></div>
<div class="form-group">
<button type="submit" class="btn btn-main fa fa-search">Search Car</button>
</div>
SearchResult.php
<?php $sql = "SELECT tblvehicles.*,tblbrands.BrandName,tblbrands.brandID as bid from tblvehicles join tblbrands on tblbrands.brandID=tblvehicles.VehiclesBrand.*.
tbllocations.LocationName,tbllocations.locID as lid from tblvehicles join tbllocations on tbllocations.locID=tblvehicles.Location";
$query = $dbh->prepare($sql);
$query->bindParam(':brand', $brand, PDO::PARAM_STR);
$query->bindParam(':location', $location, PDO::PARAM_STR);
$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
$cnt = 1;
if ($query->rowCount() > 0) {
foreach ($results as $result) {?>
I have tried joining the tables but every time I search, it does not display any results.
Kindly help