我已经在mysql数据库中创建了这个用于搜索的php代码。但是,我有复选框部分的问题。没有复选框部分它可以正常工作,但用它说“没有找到数据”。复选框的部分称为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>
`
答案 0 :(得分:0)
我认为您的复选框应该是具有相同名称(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>
答案 1 :(得分:0)
开始另一篇文章以保持清洁...
您的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-typ
和unfalleinschluss
列仅包含您显示的特定字符串,则不需要LIKE
您可以使用'=',但是,如果您要查找字符串IN值,我建议使用%通配符LIKE'%search_string%'。