所以我正在尝试为 sql 查询应用过滤器,该过滤器根据所选制造商显示磁轮。
到目前为止,它为 html 表单生成了一个下拉列表,并且应该使用 $_POST 将其传回,然后应用于 sql 查询以重新显示页面。我有类似的工作,但它使用数组和复选框而不是此表单中使用的选项>框。
目前它不应用任何过滤器,实际上打破了生成要显示的轮子的循环......
// Getting manufacturer to create the filter list.
$filterManu = "SELECT DISTINCT manufacturer FROM wheels";
$filterManuResult = mysqli_query($db, $filterManu) or die("Error in Selecting " . mysqli_error($db));
// Applying the filtering
if (isset($_POST['manu'])) {
$option = $_POST['manu'];
$featuredsql = "SELECT * FROM wheels WHERE manufacturer = " . $option . " ORDER BY diam";
}
$featured = $db->query($featuredsql);
生成制造商列表以供选择并提交表单帖子的表单。
<form name="manuFilter" method="POST">
<div class="form-group"></div>
<select name="manu">
<option selected disabled>Select a manufacturer</option>
<?php while($rowManu = mysqli_fetch_assoc($filterManuResult)) : ?>
<option name="manu" value="<?php echo $rowManu['manufacturer']; ?>"><?php echo $rowManu['manufacturer']; ?></option>
<?php endwhile; ?>
</select>
<div class="form-group"></div>
<button type="submit" name="filterOptions" value="displayManu" class="btn btn-default btn-sm btn-primary"><i class="fa fa-pencil"></i> Apply</button>
</form>
最佳答案
有两件事要更新:
$featuredsql = "SELECT * FROM wheels WHERE manufacturer = '" . $option . "' ORDER BY diam";<br>
(我加了一个“'”)
和选项采用以下语法:
<option value="option_value">bla bla bla
bla bla bla 是名字
关于javascript - 使用 PHP 使用 HTML 表单选项选择器过滤 MYSQL 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37130004/