【发布时间】:2015-12-27 21:39:56
【问题描述】:
我一直在一个网站上使用这个搜索功能,它从发布的单词中提取单词并搜索数据库。 Iv 对其进行了调整,以便它可以接受来自网站另一部分的内容,这些内容通过 GET 发送给它。我遇到的问题是,当您向它发送多个单词时,例如 searchstock=some search words 它不起作用。如果我只发送单个单词,例如使用 GET 的 searchstock=word,它工作得很好,它只有在发送多个单词时它不起作用,只会在数据库中显示所有结果。奇怪的是多词搜索可以通过 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()){
【问题讨论】:
-
不确定,但在 get 方法中只允许使用 ascii 字符。也许这就是原因
-
我记得空间不是 ascii 字符..