PDA

View Full Version : Creating a search box for site


sydesign
01-31-2012, 07:58 PM
I am trying to teach myself through tutorials how to create a search box for a website for a client i have. Going through on tutorial it seems to work except i keep getting errors when i search for something on the site as if i didn't create the item in mysql database. I am not familiar with mysql or php too much can some see if there is a problem with my php? or have an ideas how i could have messed up my database set up?

the form is this:

<form action="./results.php" method="get">
<input name="input" type="text" size="30" <?php echo $_GET ['input']; ?> />
<input type="submit" name="search" value="Search Items" />
</form>

and here is the connected php code:

<?php
$input = $_GET['input'];
$terms = explode(" ", $input);
$query = "SELECT * FROM search LIMIT ";

foreach ($terms as $each) {
$i++;
if ($i == 1)
$query .= "keywords LIKE '%each%' ";
else
$query .= "OR keyword LIKE '%each%' ";
}

//Connect to Database
mysql_connect("mysql5.000webhost.com", "a1894269_marksy","cousin13");
mysql_select_db("a1894269_search");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0) {

while ($row = mysql_fetch_assoc($query)) {
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];

echo "<h2><a href='$link'>$title</a></h2>
$description<br/><br/>";

}
}

else
echo "No result found for '<b>$input</b>'";

//Disconnect
mysql_close();

?>

thank you for any help!