0

我正在尝试使用 php 和 mysql 创建一个简单的搜索脚本。我有 html 选择标签,它是

  1. 人们
  2. 国家
  3. 地区
  4. 目的地

有了这个,我从 mysql 数据库中获取内容。所以以下是我的 php 脚本。

if(isset($_GET['Submit']) && $_GET['Submit'] == "Search")
{
$people = mysql_real_escape_string(htmlspecialchars(trim($_GET['people'])));
$country =  mysql_real_escape_string(htmlspecialchars(trim($_GET['country'])));
$region =  mysql_real_escape_string(htmlspecialchars(trim($_GET['region-depart'])));
$destination = mysql_real_escape_string(htmlspecialchars(trim($_GET['destination'])));
$from = mysql_real_escape_string(htmlspecialchars(trim($_GET['from'])));
$to = mysql_real_escape_string(htmlspecialchars(trim($_GET['to'])));

if(isset($people))
{

$search = mysql_query("SELECT * FROM property_step1 WHERE pro_no_sleep LIKE 
'%$people%'");
$num = mysql_num_rows($search);

while($result = mysql_fetch_array($search))
    {
        $propertyid = (int) $result['propertyid'];          
        echo $country_d = $result['pro_country'];
        echo $region_d = $result['pro_state'];
        echo $destination_d = $result['pro_city'];

    }
}

elseif(isset($country))
{
$search2 = mysql_query("SELECT * FROM property_step1 WHERE pro_country LIKE 
'%$country%'");
$num = mysql_num_rows($search2);        

while($result2 = mysql_fetch_array($search2))
    {
        $propertyid = (int) $result2['propertyid'];         
        echo $country_d = $result2['pro_country'];
        echo $region_d = $result2['pro_state'];
        echo $destination_d = $result2['pro_city'];

    }
}
else
{
    echo "nope";
}       
}

好吧,如果我选择人员(其值为 1、2、3 等),它会显示数据库中的内容,但是当我选择国家/地区时,它不会显示任何内容。我的查询有什么问题吗?

4

3 回答 3

1

isset($people)总是评估为true; 你需要检查它是否也不empty是:

if (isset($people) && !empty($people)) {
    // ...
}
于 2012-12-01T15:03:57.973 回答
0

您的国家/地区的elseif条件正在产生问题,将其替换为if only,只写if...elseif 一个条件将被执行。

使用此代码

if (isset($_GET['Submit']) && $_GET['Submit'] == "Search") {
    $people = mysql_real_escape_string(htmlspecialchars(trim($_GET['people'])));
    $country = mysql_real_escape_string(htmlspecialchars(trim($_GET['country'])));
    $region = mysql_real_escape_string(htmlspecialchars(trim($_GET['region-depart'])));
    $destination = mysql_real_escape_string(htmlspecialchars(trim($_GET['destination'])));
    $from = mysql_real_escape_string(htmlspecialchars(trim($_GET['from'])));
    $to = mysql_real_escape_string(htmlspecialchars(trim($_GET['to'])));

    if (isset($people)) {
        $search = mysql_query("SELECT * FROM property_step1 WHERE pro_no_sleep LIKE 
'%$people%'");
        $num = mysql_num_rows($search);

        while ($result = mysql_fetch_array($search)) {
            $propertyid = (int) $result['propertyid'];
            echo $country_d = $result['pro_country'];
            echo $region_d = $result['pro_state'];
            echo $destination_d = $result['pro_city'];
        }
    } 
    if (isset($country)) {
        $search2 = mysql_query("SELECT * FROM property_step1 WHERE pro_country LIKE 
'%$country%'");
        $num = mysql_num_rows($search2);

        while ($result2 = mysql_fetch_array($search2)) {
            $propertyid = (int) $result2['propertyid'];
            echo $country_d = $result2['pro_country'];
            echo $region_d = $result2['pro_state'];
            echo $destination_d = $result2['pro_city'];
        }
    } else {
        echo "nope";
    }
}
于 2012-12-01T15:03:12.433 回答
0

您正在定义每个变量,因此所有变量将始终“被设置”。

if(isset($people))将始终运行,因为它被定义为isset($country)永远不会运行。

这需要更改为:

if(!empty($people)){

}
if(!empty($country)){

}
于 2012-12-01T15:04:32.310 回答