我想向我的網站添加搜索功能。現在它非常簡單,搜索只是使用select從MySQL中獲取結果。問題是,當我在本地主機上測試它時,搜索功能非常好,但當我將它放到服務器上時,搜索功能根本無法工作。當它被使用時,它只是選擇整個數據庫。我相信問題是變量沒有傳遞到搜索結果頁面,但我不知道。搜索功能適用於本地主機,但不適用於服務器
下面是搜索框的代碼。這是所謂的「header.php中」引用的文件:
<html>
<head>
<body>
<div class="title"><img style="width:100%;" src="title.gif" /></div>
<div class="search">Search:
<form style="display:inline" name="search" action="search.php" method="get"><?php echo" <input type='text' name='query' />";?>
<a href="javascript: searchSubmit()"><img src="seo.png" alt="Search" title="Search" /> </a></form></div>
<ul class="menu">
<li><a href="index.php#home">home</a></li>
<li><a href="submit.php?title=&desc=">post an idea</a></li>
<li><a href="#about">about us</a></li>
<li><a href="#contact">contact us</a></li>
<li><a href="login.php">log in</a></li>
</ul>
</body>
</html>
,這裏是從的search.php代碼:
<?php
$search=$_GET['query'];
$search=mysql_real_escape_string($search);
?>
<html>
<head>
<?php
echo "<title>Search: $search</title>";
?>
<link rel="stylesheet" type="text/css" href="mainstyle.css" />
<script type="text/javascript" src="validate.js"></script>
</head>
<body>
<div class="title"><img style="width:100%;" src="title.gif" /></div>
<div class="search">Search:
<form style="display:inline" name="search" action="search.php" method="get"><?php echo" <input type='text' name='query' value='$search'/>";?>
<a href="javascript: searchSubmit()"><img src="seo.png" alt="Search" title="Search" /></a></form></div>
<ul class="menu">
<li><a href="index.php#home">home</a></li>
<li><a href="submit.php?title=&desc=">post an idea</a></li>
<li><a href="#about">about us</a></li>
<li><a href="#contact">contact us</a></li>
<li><a href="login.php">log in</a></li>
</ul>
<?php
mysql_connect("localhost","root","root") or die(mysql_error());
mysql_select_db("date_ideas") or die(mysql_error());
$result=mysql_query("SELECT * FROM ideas WHERE title LIKE '%".$search."%' ORDER BY post_date DESC");
echo "SELECT * FROM ideas WHERE title LIKE '%".$search."%' ORDER BY post_date DESC";
echo "<span id='none'>";
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
echo "<h2 class='center'><a href='ideaview.php?id=$id' title='View ".$row['title']." Description'>" . $row['title'] . "</a><br/>";
echo "<span class='date'>";
$date=$row['post_date'];
$time=time();
if (($time-$date)<120)
{
echo "Posted 1 minute ago";
}
else if (($time-$date)>120&&($time-$date)<3600)
{
$minutes=($time-$date)/60;
echo ("Posted " . round($minutes) . " minutes ago");
}
else if (($time-$date)>3600&&($time-$date)<7200)
{
echo "Posted 1 hour ago";
}
else if (($time-$date)<86400&&($time-$date)>7200)
{
$hours=($time-$date)/3600;
echo ("Posted " . round($hours) . " hours ago");
}
else if (($time-$date)<172800&&($time-$date)>86400)
{
echo "Posted 1 day ago";
}
else
{
echo ("Posted on " . date("m-d-y",$date));
}
echo "</span></h2>";
}
echo "</span>";
?>
<script type="text/javascript">
document.getElementById("none");
if (none.innerHTML == "")
{
none.innerHTML="<p class='center'>There are no ideas matching your search</p>";
}
</script>
</body>
</html>
最後,下面是用於提交表單的JavaScript代碼:正如我所說的,當我在我的電腦上使用它在本地主機上時,它完美地工作,只返回標題匹配搜索的行。但是,當我將它放在服務器上時,它將返回表中的所有行。
請提供完整的和格式化的代碼 – strauberry 2011-03-25 16:20:04
您不給'searchSubmit()'的正文,這是很重要的。 – Jon 2011-03-25 16:23:24
好的,對不起,我是新手,我認爲較少的代碼會更容易閱讀。我會發布完整的代碼。 – FrizbeeFanatic14 2011-03-25 16:28:47