【问题标题】:How to make search results appear on another page?如何让搜索结果出现在另一个页面上?
【发布时间】:2011-08-25 18:59:04
【问题描述】:

我在我的主页上进行了大量搜索,当用户在文本字段中键入并单击提交时,我希望我的数据库中的结果出现在另一个站点上,在这种情况下是“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>

搜索结果.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>

谢谢!

【问题讨论】:

  • 什么不起作用?你遇到了什么错误?
  • 我没有收到任何错误,它只是空白,searchresults.php 页面上没有任何内容
  • 我是否必须拆分php代码并将一些放入really.html中?
  • 要么检查错误日志,要么将显示错误设置为 1:ini_set( 'display_errors',1);
  • @James:启用 PHP 的错误报告。你会发现页面有错误。

标签: php search


【解决方案1】:

您应该将 attr name 添加到您的输入中。 例如,

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

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

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

【讨论】:

  • 但是现在:array(4) { ["search"]=> string(7) "trainee" ["searchterm"]=> string(6) "london" ["x"]= > string(1) "7" ["y"]=> string(2) "17" } 与结果一起返回到页面顶部 我该如何停止呢?
  • 看来你运行的是 var_dump($_POST);
【解决方案2】:

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

if ($result) {

试试

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

在您的查询中尝试...

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多