搜索查询无法在PHP中工作

时间:2014-04-10 08:08:51

标签: php mysql

当我使用空字段(String)搜索时,我的搜索查询无效,它返回所有结果

以下是代码:

if(isset($_GET['submit1']))
{
    $skills=trim($_GET['skillset']);
    $city=trim($_GET['city']);
    $state=trim($_GET['state']);
    $expinmonth=trim($_GET['expinmonth']);

    $result = mysqli_query($dbc,"select * from seeker where (skillset like '%$skills%') or (city like '%$city%') or (state like '%$state%') or  (expinmonth like '%$expinmonth%')");          

    if(!mysqli_num_rows($result) == 0)
    {
        while($data = mysqli_fetch_array($result))
        { 
            echo .$data['fname'].' '.$data['lname']; 
        }
    }
    else 
    {
        echo 'No Results';
    }
}

2 个答案:

答案 0 :(得分:1)

我怀疑你要做的是在跳过某个字段时检查空白字段。在这种情况下,这样的事情: -

<?php

if(isset($_GET['submit1']))
{
    $skills = mysqli_real_escape_string($dbc, trim($_GET['skillset']));
    $city = mysqli_real_escape_string($dbc, trim($_GET['city']));
    $state = mysqli_real_escape_string($dbc, trim($_GET['state']));
    $expinmonth = mysqli_real_escape_string($dbc, trim($_GET['expinmonth']));

    $result = mysqli_query($dbc,"select * 
                                from seeker 
                                where ".(($skills == '' ) ? "skillset = ''" : "skillset like '%$skills%'")."
                                or ".(($city == '' ) ? "city = ''" : "city like '%$city%'" )."
                                or ".(($state == '' ) ? "state = ''" : "state like '%$state%'")."
                                or ".(($expinmonth == '' ) ? "expinmonth = ''" : "expinmonth like '%$expinmonth%'")." ");          

    if(!mysqli_num_rows($result) == 0)
    {
        while($data = mysqli_fetch_array($result))
        { 
            echo .$data['fname'].' '.$data['lname']; 
        }
    }
    else 
    {
        echo 'No Results';
    }
}

?>

除了DrCopyPaste的评论之外,如果你只想忽略空白输入: -

<?php

if(isset($_GET['submit1']))
{
    $skills = mysqli_real_escape_string($dbc, trim($_GET['skillset']));
    $city = mysqli_real_escape_string($dbc, trim($_GET['city']));
    $state = mysqli_real_escape_string($dbc, trim($_GET['state']));
    $expinmonth = mysqli_real_escape_string($dbc, trim($_GET['expinmonth']));

    $result = mysqli_query($dbc,"select * 
                                from seeker 
                                where 1=2
                                ".(($skills != '' ) ? " OR skillset like '%$skills%'" : "")."
                                ".(($city != '' ) ? " OR city like '%$city%'" : "")."
                                ".(($state != '' ) ? " OR state like '%$state%'" : "")."
                                ".(($expinmonth != '' ) ? " OR expinmonth like '%$expinmonth%'" : "")." ");          

    if(!mysqli_num_rows($result) == 0)
    {
        while($data = mysqli_fetch_array($result))
        { 
            echo .$data['fname'].' '.$data['lname']; 
        }
    }
    else 
    {
        echo 'No Results';
    }
}

?>

答案 1 :(得分:0)

尝试这样......只是对您的查询进行一点改动

if(isset($_GET['submit1']))
{
    $skills=trim($_GET['skillset']);
    $city=trim($_GET['city']);
    $state=trim($_GET['state']);
    $expinmonth=trim($_GET['expinmonth']);

    $result = mysqli_query($dbc,"select * from seeker where (skillset like '%$skills%' and skillset <> '') or (city like '%$city%' and city <> '') or (state like '%$state%' and state <> '') or  (expinmonth like '%$expinmonth%' and expinmonth <> '')");          

    if(!mysqli_num_rows($result) == 0)
    {
        while($data = mysqli_fetch_array($result))
        { 
            echo .$data['fname'].' '.$data['lname']; 
        }
    }
    else 
    {
        echo 'No Results';
    }
}