0

这段代码让我查看报告的所有问题,它们按日期排序(最旧的在上面)。我需要插入一些东西来让我查看每个类别中的问题。类别由Categorie_ID 标识。有什么帮助吗?

    <?php
$db=mysql_connect("localhost","username","password") or die("Kan niet verbinden: ".mysql_error());
mysql_select_db(,$db);

$sql = "SELECT * FROM Probleem ORDER BY Datum ASC";
$resultaat  = mysql_query($sql); // voer SQL code uit

echo "<table border=1>";
echo "<tr><td><b>Categorie</b></td><td><b>Datum</b></td><td><b>Omschrijving;</b></td><td><b>Gebruiker_ID</b></td></tr>"; // bovenste regel
if ($resultaat) {
    while ($rij = mysql_fetch_array($resultaat)) {

    echo "<tr>";
    echo "<td>".$rij["Categorie_ID"]."</td>";
    echo "<td>".$rij["Datum"]."</td>";
    echo "<td>".$rij["Probleem"]."</td>";
    echo "<td>".$rij["Gebruiker_ID"]."</td>";
    echo "</tr>";
    }
}
echo "</table>";
mysql_close($db);
?>

您可以使用此代码选择类别。

<form method="post" action="<?php echo $_SERVER["../h5/PHP_SELF"] ?>">
`Selecteer een categorie:<select name="categorieid">
<?php
$db=mysql_connect(("localhost","username","password") or die("Kan niet verbinden: ".mysql_error());
mysql_select_db(",$db);

    $sql = "SELECT * FROM Categorie";

    $resultaat  = mysql_query($sql); // voer SQL code uit
    while ($rij = mysql_fetch_array($resultaat)) {
        echo "<option value=\"";
        echo $rij["Categorie_ID"]."\">";
        echo $rij["Categorienaam"]."</option>\n";
    }

mysql_close($db);
?

`

用这段代码试过了,没用,什么也没做(只显示类别)

<form method="post" action="<?php echo $_SERVER["../h5/PHP_SELF"] ?>">
Selecteer een categorie:<select name="categorieid">
<?php
$db=mysql_connect("localhost","username","password" or die("Kan niet verbinden: ".mysql_error());
mysql_select_db("",$db);

    $sql = "SELECT * FROM Categorie";

    $resultaat  = mysql_query($sql); // voer SQL code uit
    while ($rij = mysql_fetch_array($resultaat)) {
        echo "<option value=\"";
        echo $rij["Categorie_ID"]."\">";
        echo $rij["Categorienaam"]."</option>\n";
    }

mysql_close($db);

?>

<?php
if ($_POST["knop"]) {
$db=mysql_connect(("localhost","username","password") or die("Kan niet verbinden: ".mysql_error());
mysql_select_db("",$db);

$sql = "SELECT * FROM Probleem";
if(isset($_POST["categorieid"])){
    $cat_id = $_POST["categorieid"];
    //sanitize $cat_id to reduce injection risk here
    $sql .= " WHERE `categorieid_id` = $cat_id"; //use correct column name
}
$sql .= " ORDER BY Datum ASC";
$resultaat  = mysql_query($sql); // voer SQL code uit

echo "<table border=1>";
echo "<tr><td><b>Probleem_ID</b></td><td><b>Categorie</b></td><td><b>Datum</b></td><td><b>Omschrijving;</b></td><td><b>Gebruiker_ID</b></td></tr>"; // bovenste regel
if ($resultaat) {
    while ($rij = mysql_fetch_array($resultaat)) {

    echo "<tr>";
echo "<td>".$rij['Probleem_ID']."</td>";
echo "<td>".$rij['Categorie_ID']."</td>";
echo "<td>".$rij['Datum']."</td>";
echo "<td>".$rij['Probleem']."</td>";
echo "<td>".$rij['Gebruiker_ID']."</td>";
echo "</tr>";
    }
}
echo "</table>";
mysql_close($db);
}
?>
<form name="form1" method="post" action="">
  <input type="submit" name="Zoek" id="Zoek" value="zoek">
</form>
4

1 回答 1

1

除非我完全想念这里的问题:

提交表单后,将类别 ID 发布到您的脚本中,请尝试以下操作:

$sql = "SELECT * FROM Probleem";
if(isset($_POST["categorieid"])){
    $cat_id = $_POST["categorieid"];
    //sanitize $cat_id to reduce injection risk here
    $sql .= " WHERE `categorieid_id` = $cat_id"; //use correct column name
}
$sql .= " ORDER BY Datum ASC";
$resultaat  = mysql_query($sql); // voer SQL code uit

然后应该获得该类别 ID 的所有结果,如果未设置,您将获得之前获得的所有结果。

于 2012-06-12T20:59:23.627 回答