-1

我正在建立一个工作库存系统,我已经有一个搜索系统来搜索数据库。但是当我搜索名称中包含任何空格的项目时,它没有找到任何结果。这是我的脚本,任何帮助将不胜感激。

    <?php
    // we connect to our Database
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("inventory") or die(mysql_error());

    $find = $_POST['find'];

    echo "<h2>Search Results:</h2><p>";
    //If they did not enter a search term we give them an error
    if ($find == "")
    {
    echo "<p>You forgot to enter a search term!";
    exit;
    }

    // We perform a bit of filtering
    $find = strtoupper($find);
    $find = strip_tags($find);
    $find = trim ($find);

    //Now we search for our search term, in the field the user specified
    $data = mysql_query("SELECT * FROM parts WHERE vendor LIKE'%$find%' OR category
    LIKE'%$find%' OR prtid LIKE'%$find%' OR description LIKE '%$find%'");
    if (!$data) { // add this check.
    die('Invalid query: ' . mysql_error());
}
4

2 回答 2

1

%只会在整个搜索文本的外部插入通配符。如果要处理多字搜索字符串,则需要编写更复杂的查询。

于 2012-07-17T20:37:08.250 回答
0
$searchTerms = explode(' ', $find);
$searchTermBits = array();
foreach ($searchTerms as $term) {
    $term = trim($term);
    if (!empty($term)) {
        $searchTermBits[] = "vendor LIKE '%$term%'";
    }
}

then in query just implode the $searchTermBits instead of $term

于 2012-07-17T20:40:38.280 回答