Search code examples
phpsearch-engine

search engine has 2 errors when i search anything


I'm building my own search engine for my website and I keep getting two errors when i search anything the errors are listed below, the results do come in it still displays the errors. what could i do to make them disappear? by disappear i mean fix! thanks

error 1

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

error 2

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

here is my code my 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>

here is my code for 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>
";

}
}

}
}

?>

Solution

  • $x = 0;
    $construct = '';  
    

    define it before for loop