如何在php/mysql中从下拉列表中选择所有选项
感谢stackoverflow.com的专家们,我成功地创建了我的第一个php+mysql应用程序。 代码在mysql数据库中搜索姓氏和城市。通过以下两个下拉列表进行选择: 选择城市:如何在php/mysql中从下拉列表中选择所有选项,php,mysql,Php,Mysql,感谢stackoverflow.com的专家们,我成功地创建了我的第一个php+mysql应用程序。 代码在mysql数据库中搜索姓氏和城市。通过以下两个下拉列表进行选择: 选择城市: 所有城市 利物浦 曼彻斯特 选择姓氏: 所有姓氏 列侬 加拉赫 密码将返回,例如所有住在利物浦的蓝侬人 然而,我还不能选择“所有城市”和“所有姓氏”,以便代码返回,例如,所有居住在任何城市的列侬人或所有居住在利物浦的人。那么,如何做到这一点呢 迄今为止的守则: index.php <?php $conn =
所有城市
利物浦
曼彻斯特 选择姓氏:
所有姓氏
列侬
加拉赫 密码将返回,例如所有住在利物浦的蓝侬人 然而,我还不能选择“所有城市”和“所有姓氏”,以便代码返回,例如,所有居住在任何城市的列侬人或所有居住在利物浦的人。那么,如何做到这一点呢 迄今为止的守则: index.php
<?php
$conn = mysql_connect('localhost', 'user', 'password') or die("Connection failed");
mysql_select_db("database", $conn) or die("Switch database failed");
//this gets the cities from the database to the drop list
$query = "SELECT DISTINCT city FROM user".mysql_real_escape_string($city);
$result = mysql_query($query, $conn);
$options="";
while ($row=mysql_fetch_array($result)) {
$city=$row["city"];
$options.="<OPTION VALUE=\"$city\">".$city;
}
//this gets the last names from the database to the drop list
$query2 = "SELECT DISTINCT lastname FROM user".mysql_real_escape_string($lastname);
$result2 = mysql_query($query2, $conn);
$options2="";
while ($row2=mysql_fetch_array($result2)) {
$lastname=$row2["lastname"];
$options2.="<OPTION VALUE=\"$lastname\">".$lastname;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>test</title>
</head>
<body>
<form action="get.php" method="post">
<p>
<select name="city">
<option value=0>Choose
<option value=1>All cities
<?=$options?>
</select>
</p>
<p>
<select name="lastname">
<option value=0>Choose
<option value=1>All last names
<?=$options2?>
</select>
</p>
<p>
<input value="Search" type="submit">
</p>
</form>
<br>
</body>
</html>
测试
选择
所有城市
选择
所有姓氏
get.php
<?php
$conn = mysql_connect('localhost', 'user', 'password') or die("Connection failed");
mysql_select_db("database", $conn) or die("Switch database failed");
$query = "SELECT * FROM user WHERE city = '".mysql_real_escape_string($_POST['city'])."' AND lastname = '".mysql_real_escape_string($_POST['lastname'])."'";
$result = mysql_query($query, $conn);
echo $rowcount;
$zerorows=true;
while ($row = mysql_fetch_assoc($result))
{
$zerorows=false;
echo '<b>City: </b>'.htmlspecialchars($row[city]).'<br />';
echo '<b>Last name: </b>'.htmlspecialchars($row[lastname]).'<br />';
echo '<b>Information: </b>'.htmlspecialchars($row[information]).'<br />'.'<br />';
}
if($zerorows) echo "No results";
mysql_close($conn);
?>
只需为all(如all)设置一个特殊值,并输入:
if ($_POST['city'] == 'ALL')
{
$sql_city = '1';
}
else
{
$sql_city = "city = '".mysql_real_escape_string($_POST['city'])."'";
}
if ($_POST['lastname'] == 'ALL')
{
$sql_lastname = '1';
}
else
{
$sql_lastname = "lastname = '".mysql_real_escape_string($_POST['lastname'])."'";
}
$query = "SELECT * FROM user WHERE ".$sql_city." AND '".$sql_lastname."";
然后基于if构建SQL语句。使用1的原因是它的计算结果始终为true。只需为all(例如all)指定一个特殊值,并输入:
if ($_POST['city'] == 'ALL')
{
$sql_city = '1';
}
else
{
$sql_city = "city = '".mysql_real_escape_string($_POST['city'])."'";
}
if ($_POST['lastname'] == 'ALL')
{
$sql_lastname = '1';
}
else
{
$sql_lastname = "lastname = '".mysql_real_escape_string($_POST['lastname'])."'";
}
$query = "SELECT * FROM user WHERE ".$sql_city." AND '".$sql_lastname."";
然后基于if构建SQL语句。使用1的原因是它的计算结果始终为true