Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按国家/地区筛选mysql中的数据_Php_Mysql - Fatal编程技术网

Php 按国家/地区筛选mysql中的数据

Php 按国家/地区筛选mysql中的数据,php,mysql,Php,Mysql,我想从下拉菜单中按国家筛选数据: <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">Belg

我想从下拉菜单中按国家筛选数据:

<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");  
}  
?>

选择一个国家:
奥地利
比利时
保加利亚
应该是

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

其他人也是如此

使用选择标记时,服务器页面将引用选择标记的名称而不是选项

按以下方式更改代码:

<select name="country">
    <option value="AU">Austria</option>
    <option value="BE">Belgium</option>
    <option value="BU">Bulgaria</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");  
}  
?>

选择一个国家:
奥地利
比利时
保加利亚

避免编写冗余代码。 使用以下代码更改您的代码:

<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."");  
}
?>

选择一个国家:
奥地利
比利时
保加利亚

更改为
并从
中删除
name=“country”
,这样更好,尤其是当我有很多国家的时候。谢谢
<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."");  
}
?>