尝试创建PHP搜索脚本时清空搜索结果

时间:2013-08-12 09:14:20

标签: php mysql

为什么当我尝试搜索mysql数据库时,它显示0结果?我把桌子改成了FULLTEXT等。我错过了什么?我已经把非常基本的搜索练习代码放在这里了。我想搜索我的表'产品'并从那里提取细节。但没有运气

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
    $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);
    if($_POST['filter1'] == "Whole Site"){
        $sqlCommand = "SELECT product_name AS name, details AS details FROM products WHERE MATCH (product_name ,details) AGAINST ('$searchquery' IN BOOLEAN MODE)";
    } 
    require_once("storescripts/connect_to_mysqli.php");
    $query = mysqli_query($myConnection,$sqlCommand) or die(mysqli_error($myConnection));
    $count = mysqli_num_rows($query);
    if($count > 1){
        $search_output .= "<hr />$count results for <strong>$searchquery</strong><hr />$sqlCommand<hr />";
        while($row = mysqli_fetch_array($query)){

            $name = $row["name"];
                    $details= $row["details"];
            $search_output .= "Name: '.$name.' - '.$details.'<br />";
        } // close while
    } else {
        $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
    }
}
?>
<html>
<head>
</head>
<body>
<h2>Search the Exercise Tables</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Search For: 
  <input name="searchquery" type="text" size="44" maxlength="88"> 
Within: 
<select name="filter1">
<option value="Whole Site">Whole Site</option>

</select>
<input name="myBtn" type="submit">
<br />
</form>
<div>
<?php echo $search_output; ?>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:3)

改变这个:

$sqlCommand = "SELECT product_name AS name, details AS details FROM products WHERE MATCH (product_name ,details) AGAINST ('".$searchquery."' IN BOOLEAN MODE)";

我已经改变了

AGAINST ('$searchquery' IN

AGAINST ('".$searchquery."' IN 

答案 1 :(得分:0)

问题与你的if部分有关:

  1. 替换您的当前:

    if($_POST['filter1'] == "Whole Site") { $sqlCommand = "SELECT product_name AS name, details AS details FROM products WHERE MATCH (product_name ,details) AGAINST ({$searchquery} IN BOOLEAN MODE)"; }

  2. 如果由于if语句添加而无法进行查询:

    else { echo "No seqrch query found"; }

  3. 希望它现在有效