Introduction

This article explains Form Based Searching in PHP. For searching I will use two examples for demonstrating how easy it is to build search interfaces for searching MySQL table records. Many effective search interfaces involve one text field. As an example, suppose you would like to supply a keyword with the ability to show worker contact data by name.  A sample interface implements this interface, passing the requested name into the search question. If the number of result rows is greater than zero then every row is output; otherwise, the associate degree acceptable message is offered. This is a basic searching concept in PHP.

This is your table example.

search table.jpg

Example

<html>

<head>

<title>Example of search the data</title>

</head>

<body bgcolor="#00CC99">

<p>

Search the employee Records

<br/>

<form action="exa.php" method="post">

Last name

<br/>

<input type="text" name="lastname" size="25" maxlength="30" value="">

<input type="submit" value="Search!">

</form>

</p>

</body>

</html>

<?php

if (isset($_POST['lastname']))

{

$con = new mysqli("localhost", "root", "", "search");

//select the data from employee table

$comm = $con->prepare("SELECT firstname, lastname, email FROM employee

WHERE lastname=?");

$comm->bind_param('s', $_POST['lastname']);

$comm->execute();

$comm->store_result();

if ($comm->num_rows > 0)

{

$comm->bind_result($firstName, $lastName, $email);

while ($comm->fetch())

printf("%s, %s (%s)<br />", $lastName, $firstName, $email);

}

else

{

echo "No results found.";

}

}

?>

Output

If I enter the keyword "sharad" then the search produces these records from the employee table:

search1.jpg

This is your output:

search12.jpg

Next, Searching.

search2.jpg

This is your output.

search22.jpg

Example

<html>

<head>

<title>Example of search the data</title>

</head>

<body bgcolor="#00CC99">

<p>

Search the employee Records

<br/>

<form action="searchextended.php" method="post">

Keyword

<br/>

<input type="text" name="keyword" size="30" maxlength="45" value="" />

<br/>

Field

<br/>

<select name="field">

<option value="">Select field:</option>

<option value="lastname">Last name</option>

<option value="email">Email address</option>

</select>

<input type="submit" value="Search!" />

</form>

</p>

</body>

</html>

<?php

if (isset($_POST['field']))

{

$con = new mysqli("localhost", "root", "", "search");

if ($_POST['field'] == "lastname")

{

$comm = $con->prepare("SELECT firstname, lastname, email

FROM employee WHERE lastname = ?");

} elseif ($_POST['field'] == "email")

{

$comm = $con->prepare("SELECT firstname, lastname, email

FROM employee WHERE email = ?");

}

$comm->bind_param('s', $_POST['keyword']);

$comm->execute();

$comm->store_result();

if ($comm->num_rows > 0)

{

$comm->bind_result($firstName, $lastName, $email);

while ($comm->fetch())

printf("%s, %s (%s)<br />", $lastName, $firstName, $email);

}

 else

{

echo "No results found.";

}

}

?>

Output

Here, you will enter the keyword "sharad" then choose the field then click on the search button.

search3.jpg

This is your output.

search31.jpg

But if you enter the keyword "kumar" then show your data from the employee table then:

search32.jpg

This is your output.

search321.jpg

Next Recommended Readings