搜索引擎 - 如何处理空查询

时间:2015-01-20 00:25:58

标签: javascript php html

这个小组的新手。需要提示忽略空搜索框查询。以下代码工作正常,但是当我单击文本字段中没有任何内容的搜索按钮时,它会将我的所有页面链接作为结果提供给我。我想要一个空盒子搜索没有动作......先谢谢你们!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
    <form action='./search.php' method='get'>       
        <input type='text' name='k' size='15' value='<?php echo isset($_GET['k']) ? $_GET['k'] : ""; ?>' placeholder=' ' />
        <input type="image" src="images/magnifier.png"  width="14"  height="14" border="0" >
    </form>


    <h2>Search Results</h2>
    <hr />
    <?php

        $k = isset($_GET['k']) ? $_GET['k'] : "";

        $terms = explode(" ", $k);
        $query = "SELECT * FROM search WHERE ";

        $i = 0;
        foreach ($terms as $each){
            $i++;               
            if ($i == 1)
                $query .= "keywords LIKE '%$each%' ";
            else
                $query .= "OR keywords LIKE '%$each%' ";
        }


        // connect to DB
        mysql_connect("localhost", "root", "mypwd");
        mysql_select_db("busqueda");

        $query = mysql_query($query);
        $numrows = mysql_num_rows($query);
        if ($numrows > 0) {

            while ($row = mysql_fetch_assoc($query)) {
                $id = $row['id'];
                $title = $row['title'];
                $description = $row['description'];
                $keywords = $row['keywords'];   
                $link = $row['link'];   

                echo "<h2><a href='$link'>$title</a></h2>
                $description<br /><br />";  

                    //empty query variable before coming back to home page
                    $k = null;

            }
        }
        else
            echo "No results found for \"<b>$k</b>\"";
            //empty query variable before coming back to home page
                    $k = null;

            //disconnect from DB
            mysql_close();

    ?>
</body>
</html>

4 个答案:

答案 0 :(得分:0)

如果您获得空查询,请不要做任何事情

if (empty($_GET['k']) or trim($_GET['k']) == ''){
    exit;// or something
}

您也可以阻止表单提交带有必需属性的空查询

<form action='./search.php' method='get'>       
    <input type='text' name='k' required size='15' value='<?php echo isset($_GET['k']) ? $_GET['k'] : ""; ?>' placeholder=' ' />
    <input type="image" src="images/magnifier.png"  width="14"  height="14" border="0" >
</form>

答案 1 :(得分:0)

之前

$terms = explode(" ", $k);

检查$ k是否为空

if($k !=''){
// put here your query structure and database stuff 
// now    we are sure that there is some content in the k field 

}

答案 2 :(得分:0)

在使用if和if语句定义$ k之后,只需包装其余代码:

if($k != ""){
   //execute search
}

答案 3 :(得分:0)

如果需要=&#39; true&#39;是不够的(在客户端),因为你可能想要阻止提交(代码假定jQuery使用)并做一些事情:

$(document).ready(function() {
    $('form').submit(function() { // replace by form #id selector
        if ($(this).find('[name=k]').val().trim() == '') {
            return false;  // do other stuff, as warn user
        }
    });
});