Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.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,我正在尝试用php和mysql创建一个简单的搜索脚本。我有一个html选择标记,它是 人 国家 区域 目的地 从 到 通过这个,我从mysql数据库中获取内容。下面是我的php脚本 if(isset($_GET['Submit']) && $_GET['Submit'] == "Search") { $people = mysql_real_escape_string(htmlspecialchars(trim($_GET['people']))); $country = my

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

  • 国家
  • 区域
  • 目的地
  • 通过这个,我从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等等),它会显示数据库中的内容,但当我选择国家时,它不会显示任何内容。我的查询中是否有任何错误?

    您的elseif国家条件正在产生问题,请将其替换为如果仅执行,写入
    如果…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";
        }
    }
    
    isset($people)
    的计算结果总是
    true
    ;您还需要检查它是否
    为空

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

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

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

    这需要更改为:

    if(!empty($people)){
    
    }
    if(!empty($country)){
    
    }
    

    你能和我们分享你的mysql表吗?它应该达到你想要的效果,我会改变它。显示第一个查询,但不显示第二个查询。。它是show empty Page您总是需要它来运行
    人员
    国家
    ?如果是这样,您需要删除
    elseif
    语句中的
    else
    。我之所以使用elseif语句,是因为有时用户只能选择人员、国家或目的地,因此如果用户仅选择1个项目,则会根据其所选项目显示一些内容。我删除了else,但不显示任何内容,只有第一个查询正在运行。