gpt4 book ai didi

php表单复选框搜索

转载 作者:行者123 更新时间:2023-11-29 22:25:45 26 4
gpt4 key购买 nike

我已经创建了这个 php 代码,用于在 mysql 数据库中进行搜索。但是,我对复选框部分有疑问。如果没有复选框部分,它可以正常工作,但如果有它,就会显示“未找到数据”。复选框的部分称为关税类型。

`

    <?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>

`

最佳答案

开始另一篇文章以保持干净......

您的 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%' 和 % 通配符。

关于php表单复选框搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30284629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com