1

我制作了一个简单的搜索引擎,可以搜索我的数据库。它是这样工作的:您在复选框中选中要搜索的单词。结果使用表格发送。问题是它只搜索检查的最后一个单词。这意味着我要检查 3 个单词,它只显示检查的最后一个单词的结果。这是我的表格:

<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>"> 
<p>Search for: 
</p> 
Books: <input type="checkbox" name='search' value="Books"> 
Movies: <input type="checkbox" name='search' value="Movies"> 
Outdoor: <input type="checkbox" name='search' value="Outdoor"> 
Indoor: <input type="checkbox" name='search' value="Indoor"> 
</p> 
<p><input type='submit'  value='Search'></p> 
</form> 

这是我生成结果的代码:

    <?php 
if(isset($_POST['search'])) 
{ 
  $connx = mysql_connect('localhost', 'USER', 'PASSWORD') or die("connx"); 
  $db = mysql_select_db('DB_NAME') or die(mysql_error()); 

  # convert to upper case, trim it, and replace spaces with "|": 
  $search = (ini_get('magic_quotes_gpc')) ? stripslashes($_POST['search']) : 
            $_POST['search']; 
  $search = mysql_real_escape_string($search); 
  $search = strtoupper(preg_replace('/\s+/', '|', trim($_POST['search']))); 

  # create a MySQL REGEXP for the search: 
  $regexp = "REGEXP '[[:<:]]($search)[[:>:]]'"; 
  $query = "SELECT * FROM `galleries` WHERE UPPER(`keywords1`) $regexp OR ". 
           "`keywords2` $regexp"; 
  $result = mysql_query($query) or die($query . " - " . mysql_error()); 

echo "<table>\n"; 
while($row = mysql_fetch_assoc($result)) 

{ 
    echo "<tr>"; 
    echo "<td><img src=../thumbs/{$row['type']}/{$row['folder']}/{$row['date']}-{$row['num']}/{$row['thumbimage']} border=1></td>";
    echo "<td>{$row['name']}</td>"; 
    echo "<td>{$row['date']}</td>"; 
    echo "<td><a href=../view.php?id={$row['id']} target=blank>View</a></td>"; 
    echo "</tr>\n"; 
  }  
} 
?> 

有人可以帮我告诉我如何让搜索引擎搜索和/或显示超过 1 个单词的搜索结果吗?

4

2 回答 2

0

问题已解决!我替换了 2 行:

$search = mysql_real_escape_string($search);

$search = strtoupper(preg_replace('/\s+/', '|', ($_POST['search'])));

和:

$search = implode('|', $_POST['search']);

现在它的工作 - 就像其他人应该需要信息:)

于 2013-11-06T18:56:52.383 回答
0

您有相同名称的输入字段,因此您获得了为搜索字段创建数组所需的最后一个字段,name="search[]"并通过您对数组值的查询进行循环

Books: <input type="checkbox" name='search[]' value="Books"> 
Movies: <input type="checkbox" name='search[]' value="Movies"> 
Outdoor: <input type="checkbox" name='search[]' value="Outdoor"> 
Indoor: <input type="checkbox" name='search[]' value="Indoor"> 

像这样的东西

$regexp ="";
$searharray =$_POST['search'];
$count=count($searharray);
$index=1;
foreach($searharray as $s){

$s = (ini_get('magic_quotes_gpc')) ? stripslashes($s) :$s; 
$s= mysql_real_escape_string($s); 
$s = strtoupper(preg_replace('/\s+/', '|', trim($s))); 

$regexp .= " UPPER(`keywords1`) REGEXP '[[:<:]](".$s.")[[:>:]]'  OR  `keywords2` REGEXP '[[:<:]](".$s.")[[:>:]]'"; 
if($index<$count){
  $regexp .=" OR ";
 }
 $index++
}

 $query = "SELECT * FROM `galleries` WHERE $regexp"; 
于 2013-10-31T19:32:32.810 回答