如何使搜索结果出现在另一个页面


How to make search results appear on another page?

我有一个大搜索在我的主页上,当用户在文本字段中键入并点击提交,我希望从我的数据库的结果出现在另一个网站在这种情况下'searchresults.php '。在这种情况下'really.html'是我的主页。

下面是我的代码:

我做错了什么?

Really.html

<center>
<form action="searchresults.php" method="post">
<input type="text" size="35" value="Job Title e.g. Assistant Manager" 
style="background-    color:white; border: 
solid 1px #6E6E6E; height: 30px; font-size:18px; 
vertical-align:9px;color:#bbb" 
onfocus="if(this.value == 'Job Title e.g. Assistant Manager'){this.value = 
'';this.style.color='#000'}" />
<input type="text" size="35" value="Location e.g. Manchester"
style="background-    color:white; border: 
solid 1px #6E6E6E; height: 30px; font-size:18px; 
vertical-align:9px;color:#bbb" 
onfocus="if(this.value == 'Location e.g. Manchester'){this.value = 
'';this.style.color='#000'}" />
<input type="image" src="but.tiff" alt="Submit" width="60">
</form>

Searchresults.php

<html>
<body>
<?php
if(strlen(trim($_POST['search'])) > 0) {
//all of your php code for the search
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
    mysql_connect ("", "", "");
      mysql_select_db ("");
    if (!empty($_POST["search_string"])) 
    { 
    }  
  $query = "SELECT name,location,msg FROM contact WHERE name LIKE '$search' AND 
location      LIKE '$searchterm'";
  $result = mysql_query ($query);
if ($result) {
    while ($row = mysql_fetch_array ($result)) {
      echo "<br>$row[0]<br>";
      echo "$row[1]<br>";
      echo "$row[2]<br>";
    }
  }
}
?>
</body>
</html>

谢谢!

您应该在输入中添加attr name。例如

<input type="text" name="search" ... />
<input type="text" name="searchterm" ... />

也不要忘记使用mysqL_escape_string函数转义输入数据

查询可能返回0个结果。而不是

if ($result) {

if (mysql_num_rows($result) >= 1) {

And in your query try…

$query = "SELECT name,location,msg FROM contact WHERE name LIKE '%$search%' AND 
location      LIKE '%$searchterm%'";

这将不那么严格,并返回更好的结果集。