使用复选框使用PHP过滤MySQL查询

时间:2022-10-29 08:11:43

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.

我正在为酒店管理公司的网站工作。在投资组合页面上,我目前有一张Google地图,其中包含所有酒店的精确定位以及所有来自数据库的酒店表。但是,我想要一个带有与酒店品牌相对应的复选框的表单。选中复选框将仅显示该品牌名称的酒店。我可以弄清楚如何选择一个品牌,但不是多个品牌。

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

并且MySQL Query显示表

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

任何帮助,将不胜感激。

1 个解决方案

#1


0  

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

最简单的方法是使用WHERE ... IN()查询,而不是LIKE。例如

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

请注意,这只是一个例子。它容易受到SQL注入攻击,不应该按原样使用。

#1


0  

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

最简单的方法是使用WHERE ... IN()查询,而不是LIKE。例如

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

请注意,这只是一个例子。它容易受到SQL注入攻击,不应该按原样使用。