php 表单复选框搜索

php form checkbox search

我创建了这个 php 代码用于在 mysql 数据库中进行搜索。但是,我对复选框部分有疑问。没有复选框部分它工作正常,但它说 "no data found"。复选框的部分称为 tarif-typ。

`

    <?php

    include "db_connect.inc.php";

$sql = "SELECT * FROM praemien";
$sql .= " where kanton like '" . $_POST["kanton"] . "' and franchise = ". $_POST["franchise"] ." and ";

switch($_POST["unfall"])
{ case 1:
    $sql .="unfalleinschluss like 'OHN-UNF'";
    break;
 case 2:
    $sql .="unfalleinschluss like 'MIT-UNF'";
} 

$sql .=" and tarif-typ like '" . $_POST["tb"] . "' ";


$sql .= " order by praemie";

$res = mysqli_query($con, $sql);
$num = mysqli_num_rows($res);
if ($num==0) echo "Keine Datensätze gefunden";

while ($dsatz = mysqli_fetch_assoc($res))
echo $dsatz["versicherungsnamen"] . ", "
    .$dsatz["kanton"] . ", "
    .$dsatz["tarif-typ"] . ", "
    .$dsatz["unfalleinschluss"] . ","
    . $dsatz["praemie"] . "<br />"; 

    mysqli_close($con);

    ?>
</body>

`

这是我的 html 表格

`

<html>
    <body>
    <form action ="db_eingabe.php" method="post">
        <p><input name="kanton" /> Kanton</p>
        <p><input name="franchise" /> Franchise</p>
        <p><input type="radio" name="unfall" value="1" checked="checked" />Unfall nein<br>
        <input type="radio" name="unfall" value="2" />Unfall ja</p>
        <br><p>
        <b>Tarif</b>
        </p>

    <p><input type="checkbox" name="tb1" value="TAR-BASE" checked="checked" />Grund</p>
    <p><input type="checkbox" name="tb2" value="TAR-HMO" />HMO</p>
    <p><input type="checkbox" name="tb3" value="TAR-HAM" />HAM</p>
    <p><input type="checkbox" name="tb4" value="TAR-DIV" />andere</p>

        <p><input type="submit" /> 
        <input type ="reset" /></p>
    </form>
    </body>
</html>

`

我认为您的复选框应该是所有同名 (tb) 的收音机,因为您的表单中没有提交名为 "tb" 的字段,这就是您的查询失败的原因。

<p><input type="radio" name="tb" value="TAR-BASE" checked="checked" />Grund</p>
<p><input type="radio" name="tb" value="TAR-HMO" />HMO</p>
<p><input type="radio" name="tb" value="TAR-HAM" />HAM</p>
<p><input type="radio" name="tb" value="TAR-DIV" />andere</p>

开始另一个 post 以保持清洁...

您的PHP代码:

<?php

include "db_connect.inc.php";

$sql = "
    SELECT
        *
    FROM
        `praemien`
    WHERE
        `kanton` LIKE '" . $_POST["kanton"] . "'
        AND `franchise` = '". $_POST["franchise"] ."'
        AND `unfalleinschluss` LIKE '" . $_POST["unfall"] . "'";

$tbs = array();

foreach( array( 'tb1', 'tb2', 'tb3', 'tb4' ) as $tb_key )
{
    if ( empty( $_POST[$tb_key] ) ) continue;

    $tbs[] = "`tarif-typ` LIKE '" . $_POST[$tb_key] . "'";
}
if ( !empty( $tbs ) )
{
    $sql .= ' AND ( ' . implode( ' OR ', $tbs ) . ' )';
}
$sql .= " ORDER BY praemie";

echo $sql;

$res = mysqli_query($con, $sql) or die( mysql_error() );
$num = mysqli_num_rows($res);
if ($num==0) echo "Keine Datensätze gefunden";

while ($dsatz = mysqli_fetch_assoc($res)) {
    echo $dsatz["versicherungsnamen"] . ", "
    .$dsatz["kanton"] . ", "
    .$dsatz["tarif-typ"] . ", "
    .$dsatz["unfalleinschluss"] . ","
    . $dsatz["praemie"] . "<br />";
}
mysqli_close($con);

?>

和您的 HTML 代码:

<html>
<body>
<form action ="db_eingabe.php" method="post">
    <p><input name="kanton" /> Kanton</p>
    <p><input name="franchise" /> Franchise</p>
    <p><input type="radio" name="unfall" value="OHN-UNF" checked="checked" />Unfall nein<br>
    <input type="radio" name="unfall" value="MIT-UNF" />Unfall ja</p>
    <p>
    <b>Tarif</b>
    </p>

    <p><input type="checkbox" name="tb1" value="TAR-BASE" checked="checked" />Grund</p>
    <p><input type="checkbox" name="tb2" value="TAR-HMO" />HMO</p>
    <p><input type="checkbox" name="tb3" value="TAR-HAM" />HAM</p>
    <p><input type="checkbox" name="tb4" value="TAR-DIV" />andere</p>

    <p><input type="submit" /> 
    <input type ="reset" /></p>
</form>
</body>
</html>

注意:已修​​复 franchise = '". $_POST["franchise"] ."' -- 它没有单引号

更改了 unfall 单选组以具有特定值以避免 switch 你有

最后,如果 tarif-typunfalleinschluss 列仅包含您显示的特定字符串,则不需要 LIKE 您可以使用 '=',但是,如果您需要要在值中查找字符串,我建议使用 % 通配符 LIKE '%search_string%'。