选中复选框时如何显示数据库中的特定数据

时间:2014-01-27 13:44:10

标签: php checkbox

我有一个网站,目前显示我的数据库中的数据。我也有一些复选框。现在我希望它在用户检查时只显示与这些复选框对应的行。这就是我到目前为止所做的:

复选框:

<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'] . "&nbsp;<i>Prijs: &euro;&nbsp;" . $row['Prijs'] . "    </i><br/><br/></td>"; 
echo "</tr>"; 

}

我知道我可以使用WHERE来显示我想要的数据,我只是在选中复选框时才知道该怎么做。

3 个答案:

答案 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'] . "&nbsp;<i>Prijs: &euro;&nbsp;" . $row['Prijs']."</i><br/><br/></td>"; 
 echo "</tr>"; 

}