简体   繁体   中英

Filtering MySQL Query using PHP using checkboxes

I am working on a website for a hotel management company. On the portfolio page, I currently have a Google map with all of the hotels pinpointed and a table of the hotels all coming from a database. However, I want a form with checkboxes that correspond to the hotel brands. Selecting a checkbox will show only hotels that are that brand name. I can figure out how to do it with just one brand selected, but not with multiple.

This is the code for the form:

<form>
<div class="form-block">
<input type="checkbox" name="Courtyard" value="true" <?php if (isset($_GET["Courtyard"])) { echo "checked"; } ?> /> Courtyard<br />
<input type="checkbox" name="HolidayInnExpress" value="true" <?php if (isset($_GET["HolidayInnExpress"])) { echo "checked"; } ?> /> Holiday Inn Express<br />
<input type="checkbox" name="BestWestern" value="true" <?php if (isset($_GET["BestWestern"])) { echo "checked"; } ?> /> Best Western<br />
</div>
<div class="form-block">
<input type="checkbox" name="CrownePlaza" value="true" <?php if (isset($_GET["CrownePlaza"])) { echo "checked"; } ?> /> Crowne Plaza<br />
<input type="checkbox" name="HolidayInn" value="true" <?php if (isset($_GET["HolidayInn"])) { echo "checked"; } ?> /> Holiday Inn<br />
<input type="checkbox" name="SpringhillSuites" value="true" <?php if (isset($_GET["SpringhillSuites"])) { echo "checked"; } ?> /> Springhill Suites<br />
</div>
<div class="form-block">
<input type="checkbox" name="HamptonInn" value="true" <?php if (isset($_GET["HamptonInn"])) { echo "checked"; } ?> /> Hampton Inn<br />
<input type="checkbox" name="HomewoodSuites" value="true" <?php if (isset($_GET["HomewoodSuites"])) { echo "checked"; } ?> /> Homewood Suites<br />
<input type="checkbox" name="Independent" value="true" <?php if (isset($_GET["Independent"])) { echo "checked"; } ?> /> Independent Properties<br />
</div>
<button type="submit">Filter</button>

And the MySQL Query to display the table

    <?php
if (isset($_GET["BestWestern"])) {
  echo "Brand=\"Best Western\"";
}
$result = mysql_query("SELECT Name, City, State, Website FROM markers WHERE Brand LIKE '%' ORDER BY City");
echo "<table border='1'>
<tr>
    <th>Hotel Name</th>
    <th>City</th>
    <th>State</th>
    <th>Website</th>
</tr>";
while($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['Name'] . "</td>";
    echo "<td>" . $row['City'] . "</td>";
    echo "<td>" . $row['State'] . "</td>";
    echo "<td><a href='" . $row['Website'] . "'>Visit Website</a></td>";
    echo "</tr>";
}
echo "</table>";
?>

Any help would be appreciated.

Simplest method would be use an WHERE ... IN () query, instead of the LIKE . eg

$brands = array('Brand X', 'Brand Y');
$sql = "SELECT ... WHERE Brand in ('" . implode("','", $brands) . "');";

Please note that this just an example. It is vulnerable to SQL injection attacks and should NOT BE USED as-is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM