0

作为问题,它只选择了我的 product_name 中的第一个单词,其中 var_char(50) 作为主键。

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
    $searchquery = $_POST['searchquery'];
    if($_POST['filter1'] == "Whole Site"){
    $sqlCommand = "SELECT * FROM products WHERE MATCH (product_name ,details,category,subcategory) 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)){
                $id=$row["id"];
            $product_name = $row["product_name"];
                    $details= $row["details"];
                $category=$row["category"];
                $subcategory=$row["subcategory"];
            $search_output .= "ID: $id <br/> Name: $product_name -<br/>$details<br />$category<br/>$subcategory<br/>
<a href='product.php?id=$id'>link</a><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>

这是因为它是主键吗?它实际上也不适用于细节,它实际上很奇怪,不适用于大多数单词但适用于某些单词..我完全困惑..它没有任何意义

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once("storescripts/connect_to_mysqli.php");
$search_output = "";

if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
    $searchquery = $_POST['searchquery'];
    if($_POST['filter1'] == "Whole Site"){
$sqlCommand = sprintf("SELECT * FROM products WHERE product_name LIKE '%s%%'",
               mysqli_real_escape_string($myConnection, $searchquery));
    } 

    $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)){
                $id=$row["id"];
            $product_name = $row["product_name"];
                    $details= $row["details"];
                $category=$row["category"];
                $subcategory=$row["subcategory"];
            $search_output .= "ID: $id <br/> Name: $product_name -<br/>$details<br />$category<br/>$subcategory<br/>
<a href='product.php?id=$id'>link</a><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>

我也尝试了上面的代码并缩小到product_name,它仍然没有显示,例如,如果我输入'fish',它不会显示任何东西,如果我输入'omega'然后它作品

我已经放弃了这个并尝试了 LIKE 功能,它似乎更好但仍然很有趣..请参阅此链接以供感兴趣的人参考

用于 mySQL 数据库的 PHP 搜索脚本,只有 3 个字母有效

谢谢

4

1 回答 1

0

请参考 mySQL 数据库的 PHP 搜索脚本,只有 3 个字母工作 我认为这是类似的问题,只需将 $count >1 更改为 $count =0 或 $count >=1

于 2013-08-12T13:10:09.100 回答