简体   繁体   English

按国家过滤来自mysql的数据

[英]Filter data from mysql by countries

I want to filter my data by countries from a dropdown menu:我想从下拉菜单中按国家/地区过滤我的数据:

<form name="filter_form" method="POST" action="display_data.php">
Select a country:
<select name="value">
    <option name="country" value="AU">Austria</option>
    <option name="country" value="BE">Belgium</option>
    <option name="country" value="BU">Bulgaria</option>
</select>
<input type="submit" name="btn_submit" value="Submit Filter" />
<?php
if($_POST['country'] == 'BE') {  

    $query = mysql_query("SELECT * FROM think WHERE Country='Belgium'");  
}  
elseif($_POST['country'] == 'AU') { 
 $query = mysql_query("SELECT * FROM think WHERE Country='Austria'");  
} else {  
    $query = mysql_query("SELECT * FROM think");  
}  
?>

The code does not filter any data.该代码不过滤任何数据。 If anyone can help, thanks!如果有人可以帮忙,谢谢!

When you use select tag, the Server page will refer name of select tag and not option.当您使用选择标签时,服务器页面将引用选择标签的名称而不是选项。

Change your code as below:更改您的代码如下:

<select name="country">
    <option value="AU">Austria</option>
    <option value="BE">Belgium</option>
    <option value="BU">Bulgaria</option>
</select>
if($_POST['country'] == 'BE') {  

should be应该

if($_POST['value'] == 'BE') {  

And so on for others !!等等其他人!

You need to change the location of the name attribute.您需要更改name属性的位置。 You now have it on the option element but it needs to be on the select element.您现在在option元素上有它,但它需要在select元素上。

<form name="filter_form" method="POST" action="display_data.php">
Select a country:
<select name="country">
    <option value="AU">Austria</option>
    <option value="BE">Belgium</option>
    <option value="BU">Bulgaria</option>
</select>
<input type="submit" name="btn_submit" value="Submit Filter" />
<?php
if($_POST['country'] == 'BE') {  

    $query = mysql_query("SELECT * FROM think WHERE Country='Belgium'");  
}  
elseif($_POST['country'] == 'AU') { 
 $query = mysql_query("SELECT * FROM think WHERE Country='Austria'");  
} else {  
    $query = mysql_query("SELECT * FROM think");  
}  
?>

Avoid writing redundant code.避免编写冗余代码。 Change your code with below code:使用以下代码更改您的代码:

<form name="filter_form" method="POST" action="display_data.php">
Select a country:
<select name="country">
    <option value="AU">Austria</option>
    <option value="BE">Belgium</option>
    <option value="BU">Bulgaria</option>
</select>
<input type="submit" name="btn_submit" value="Submit Filter" />

<?php
if(isset($_POST['country']))
{
    switch($_POST['country'])
    {
    case 'BE' : $countryName = 'Belgium';
                break;
    case 'AU' : $countryName = 'Austria';
                break;
    default : $countryName = '';
              break;
    }
    $where = '';
    if($countryName != '')
    {
        $where = "WHERE Country='".$countryName."'";
    }   
    $query = mysql_query("SELECT * FROM think ".$where."");  
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM