当我搜索任何内容时,搜索引擎有2个错误

时间:2014-02-18 09:50:33

标签: php search-engine

我正在为我的网站构建我自己的搜索引擎,当我搜索下面列出的任何错误时,我一直收到两个错误,结果确实显示错误。我该怎么办才能让它们消失?消失,我的意思是修复!感谢

错误1

"( ! ) Notice: Undefined variable: x in C:\wamp\www\search.php on line 21" 

错误2

"( ! ) Notice: Undefined variable: construct in C:\wamp\www\search.php on line 23"

这是我的代码我的index.php

<html>
<head>
<title>Title of your search engine</title>
</head>
<body>
<form action='search.php' method='GET'>
<center>
<h1>My Search Engine</h1>
<input type='text' size='90' name='search'></br></br>
<input type='submit' name='submit' value='Search source code' ></br></br></br>
</center>
</form>
</body>
</html>

这是我的search.php代码

<?php

$button = $_GET ['submit'];
$search = $_GET ['search']; 

if(!$button){
echo "you didn't submit a keyword";
}
else
{
if(strlen($search)<=1){
echo "Search term too short";
}else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","");
mysql_select_db("search");

$search_exploded = explode (" ", $search);

foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="keywords LIKE '%$search_each%'";
else
$construct .="AND keywords LIKE '%$search_each%'";

}

$construct ="SELECT * FROM  searchengine WHERE $construct";
$run = mysql_query($construct);

$foundnum = mysql_num_rows($run);

if ($foundnum==0){
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1. 
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
 meaning</br>3. Please check your spelling";
}else
{
echo "$foundnum results found !<p>";

while($runrows = mysql_fetch_assoc($run))
{
$title = $runrows ['title'];
$desc = $runrows ['description'];
$url = $runrows ['url'];

echo "
<a href='$url'><b>$title</b></a><br>
$desc<br>
<a href='$url'>$url</a><p>
";

}
}

}
}

?>

2 个答案:

答案 0 :(得分:1)

在将变量用于

之前声明变量

在for循环之前将其声明为

$x=0; $construct="";

答案 1 :(得分:1)

$x = 0;
$construct = '';  

在for循环

之前定义它