gpt4 book ai didi

php - 多下拉查询过滤器

转载 作者:行者123 更新时间:2023-11-30 23:32:30 25 4
gpt4 key购买 nike

编辑:我在我的原始代码中发现了一个很大的缺陷。对于我的选择,我对“full_name”和“city”使用了相同的变量名称(复制和粘贴错误)。但我仍然不确定如何输入选择语句以根据两个下拉菜单选择行。我在下面发布了我的代码的两个部分。有任何想法吗?谢谢!

下拉框代码:

<select name="name">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY full_name ORDER BY full_name";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["full_name"]."'".($row["full_name"]==$_REQUEST["full_name"] ? " selected" : "").">".$row["full_name"]." </option>";
}
?>
</select>&nbsp;&nbsp;
<label>City</label>
<select name="city">
<option value="">--</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY city ORDER BY city";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["city"]."'".($row["city"]==$_REQUEST["city"] ? " selected" : "").">".$row["city"]."</option>";
}
?>
</select>

并选择行代码:

<?php

if ($_REQUEST["city"]<>'') {
$search_city = " WHERE city='".mysql_real_escape_string($_REQUEST["city"])."'";
}
if ($_REQUEST["full_name"]<>'') {
$search_full_name = " AND full_name='".mysql_real_escape_string($_REQUEST["full_name"])."'";
}

if ($_REQUEST["from"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"].$search_string.$search_city.$search_full_name;
} else if ($_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"].$search_string.$search_city.$search_full_name;
} else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"].$search_string.$search_city.$search_full_name;
}

$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
?>

最佳答案

乍一看,您只需要附加字符串

$search_city = '';
$search_full_name = '';

if ($_REQUEST["city"]<>'') {
$search_city = " AND city='".mysql_real_escape_string($_REQUEST["city"])."'";
}
if ($_REQUEST["full_name"]<>'') {
$search_full_name = " AND full_name='".mysql_real_escape_string($_REQUEST["full_name"])."'";
}

if ($_REQUEST["from"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE from_date >= '".mysql_real_escape_string($_REQUEST["from"])."'".$search_string.$search_city.$search_full_name;
} else if ($_REQUEST["to"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE to_date <= '".mysql_real_escape_string($_REQUEST["to"])."'".$search_string.$search_city.$search_full_name;
} else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0".$search_string.$search_city.$search_full_name;
}

$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {

关于php - 多下拉查询过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9845778/

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