Tuesday, 25 October 2011

OWN Search Page In PHP Tutorial

This tutorial will show you how to make a search page.
Firstly we make a form. I am going to use this:

HTML:

<form action="searchpage.php" method="post">Please enter keyword.
<input maxlength="25" name="word" size="25" type="text" /> <input name="Submit" type="submit" value="Submit" /> </form>


This is the form where we will enter the keywords. Now the next step is to make the searchpage.php.
It will look like this

PHP:

// connect to db
$db = mysql_connect("localhostt", "username", "password");
mysql_select_db("dbname",$db);
//check something is sent
if ($_POST['word']) {
$word= $_POST["word"];
// search query
$result = mysql_query("SELECT * FROM `table` WHERE `field` LIKE '%$word%'");
$postword = $_POST['word'];
echo "Search Results for: <strong> $postword </strong>
";
echo"
";
echo"
";
echo"
";
echo"
";
//get results
while($get=mysql_fetch_array($result))
{
$date = $get['date'];
$name = $get['name'];
$title= $get['title'];
//show results
echo("<strong>$title</strong> was added on <strong>$date</strong> by <strong>$name</strong>");
echo"
";
echo"
";
}
// if nothing is sent
}else {
die ("No search word specified");
}
?>


0 comments:

Post a Comment