搜索功能不适用于多个单词

时间:2015-12-27 21:39:56

标签: php mysql search post get

我一直在网站上使用此搜索功能,它从发布的单词中搜索单词并搜索数据库。 Iv对它进行了调整,以便它可以接受通过GET发送给它的网站另一部分的内容。我遇到的问题是,当你发送多个单词,如searchstock =一些搜索词时,它不会工作。如果我发送它只是单个单词,如searchstock = word使用GET它工作得很好,它只有当发送多个单词时它不起作用,只是在数据库中显示所有结果。奇怪的是POST的多个单词搜索工作正常。

    // Begin Search Section >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    function search($db) {
        if (isset($_POST['searchstock'])){$words = $_POST['searchstock'];}
        if (isset($_GET['searchstock'])){$words = $_GET['searchstock'];}

    $searchQuery = ''; // search query is empty by default
    $searchCondition = "(cultivar LIKE '%%' OR description LIKE '%%' OR species LIKE '%%' OR colour LIKE '%%')";
    $searchFieldName = 'cultivar'; // name of the field to be searched
    $searchFieldName2 = 'description';
    $searchFieldName3 = 'species';
    $searchFieldName4 = 'colour';
    if(isset($_POST['searchstock']))
    { // check if a query was submitted
    $searchQuery = trim($_POST['searchstock']); // getting rid of unnecessary white space
    $searchTerms = explode(" ", $searchQuery); // Split the words
    $searchCondition = "($searchFieldName LIKE '%" . implode("%' OR $searchFieldName LIKE '%", $searchTerms) . "%')"; // Forming the condition for the sql
    $searchCondition .= " OR ($searchFieldName2 LIKE '%" . implode("%' OR $searchFieldName2 LIKE '%", $searchTerms) . "%')";
    $searchCondition .= " OR ($searchFieldName3 LIKE '%" . implode("%' OR $searchFieldName3 LIKE '%", $searchTerms) . "%')";
    $searchCondition .= " OR ($searchFieldName4 LIKE '%" . implode("%' OR $searchFieldName4 LIKE '%", $searchTerms) . "%')";
    }
    else if(isset($_GET['searchstock']))
    { // check if a query was submitted
    $searchQuery = trim($_GET['searchstock']); // getting rid of unnecessary white space
    $searchTerms = explode(" ", $searchQuery); // Split the words
    $searchCondition = "($searchFieldName LIKE '%" . implode("%' OR $searchFieldName LIKE '%", $searchTerms) . "%')"; // Forming the condition for the sql
    $searchCondition .= " OR ($searchFieldName2 LIKE '%" . implode("%' OR $searchFieldName2 LIKE '%", $searchTerms) . "%')";
    $searchCondition .= " OR ($searchFieldName3 LIKE '%" . implode("%' OR $searchFieldName3 LIKE '%", $searchTerms) . "%')";
    $searchCondition .= " OR ($searchFieldName4 LIKE '%" . implode("%' OR $searchFieldName4 LIKE '%", $searchTerms) . "%')";
    }
    // the rest is just database connection and retrieving the results
    $sql = <<<SQL
    SELECT * FROM stock WHERE $searchCondition;
    SQL;
    if(!$result = $db->query($sql)){ die('There was an error running the query [' . $db->error . ']');}
    while($row = $result->fetch_assoc()){        //Show your results here eg: $row['field1']
    $searchid = $row['id']; 
    $searchgenusid = $row['genusid'];
    // End Search
            // Search DB Function Start //
    $sql4 = <<<SQL
    SELECT * FROM `stock` WHERE `id` = '$searchid';
    SQL;
    if(!$stockresult = $db->query($sql4)){ die('There was an error running the query [' . $db->error . ']');}
    while($stockrow = $stockresult->fetch_assoc()){ 

1 个答案:

答案 0 :(得分:0)

removeUbiquitousContentAndPersistentStoreAtURL