Php Mysql搜索问题

时间:2012-12-01 14:59:23

标签: php mysql

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

  1. 国家
  2. 区域
  3. 目的地
  4. 来自
  5. 有了这个,我从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等),当我选择国家/地区时,它会显示数据库的内容它没有显示任何内容。我的查询有什么问题吗?

3 个答案:

答案 0 :(得分:1)

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

if (isset($people) && !empty($people)) {
    // ...
}

答案 1 :(得分:0)

国家/地区的 elseif 条件正在创建问题仅用 if 替换它,写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";
    }
}

答案 2 :(得分:0)

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

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

这需要更改为:

if(!empty($people)){

}
if(!empty($country)){

}