我有一个网站,目前显示我的数据库中的数据。我也有一些复选框。现在我希望它在用户检查时只显示与这些复选框对应的行。这就是我到目前为止所做的:
复选框:
<form>
Actie<input type="checkbox" name="genre" value="actie">
Sport<input type="checkbox" name="genre" value="sport">
<input type="submit" name="formSubmit" value="Submit">
</form>
我如何显示数据库中的内容:
$con->set_charset("utf8");
$result = mysqli_query($con,"SELECT Product,Prijs,Beschrijving FROM Producten order by Product ASC LIMIT 0, 5");
echo '<table border="1px solid black" cellspacing="0" style="margin-top:47px"><tbody>';
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td rowspan='2' width= '200'>" . $row['Product'] . "</td>";
echo "<td><b>" . $row['Product'] . "</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['Beschrijving'] . " <i>Prijs: € " . $row['Prijs'] . " </i><br/><br/></td>";
echo "</tr>";
}
我知道我可以使用WHERE来显示我想要的数据,我只是在选中复选框时才知道该怎么做。
答案 0 :(得分:0)
您可以检查流派是否有值或已设置。
if ($_POST['genre'] != "")
{
...
}
或
if(isset($_POST['genre']))
{
...
}
答案 1 :(得分:0)
你需要这样的东西:
$sql = "SELECT Product,Prijs,Beschrijving FROM Producten";
if(isset($_POST['genre'])){
$sql .= " WHERE Producten.`genre` IN (";
$genres = implode(',', $POST['genre']);
$genres = "'" . str_replace(",", "','", $genres) . "'";
$sql .= $genres;
$sql .= ")";
}
$sql .= " order by Product ASC LIMIT 0, 5";
$result = mysqli_query($con, $sql);
其次,您需要替换
Actie<input type="checkbox" name="genre" value="actie">
Sport<input type="checkbox" name="genre" value="sport">
与
Actie<input type="checkbox" name="genre[]" value="actie">
Sport<input type="checkbox" name="genre[]" value="sport">
答案 2 :(得分:0)
试试这个:
if($_POST['genre']!=""){
$query="SELECT Product,Prijs,Beschrijving FROM Producten order by WHERE genre=".$_POST['genre']." Product ASC LIMIT 0,5";
}
else{
$query="SELECT Product,Prijs,Beschrijving FROM Producten order by Product ASC LIMIT 0,5";
}
$con->set_charset("utf8");
$result = mysqli_query($con,$query);
echo '<table border="1px solid black" cellspacing="0" style="margin-top:47px"><tbody>';
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td rowspan='2' width= '200'>" . $row['Product'] . "</td>";
echo "<td><b>" . $row['Product'] . "</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['Beschrijving'] . " <i>Prijs: € " . $row['Prijs']."</i><br/><br/></td>";
echo "</tr>";
}