Simple User Search in PHP

A friend of mine asked me to give him a sample code of some sort for search. He’s a newbie to PHP. I wrote him a piece of code for convenient search and he was like ‘AWSOME!!!’. I thought it was such a simple script that it took me less than 5 minutes to type.. and he was still very happy…. so that gave me an idea to publish it here… maybe it could help some newbies looking for simple search scripts.

/* ======== PHP PART ========== */
if($_POST['Search']){
	$searchclause = "" ;
	if($_POST['user_name']){
		$searchClause .= " AND name LIKE '%".$_POST['user_name']."%'" ;
	}
	if($_POST['email']){
		$searchClause .= " AND email LIKE '%".$_POST['email']."%'" ;
	}
	if($_REQUEST['country']){
		$searchClause .= " AND country = '".$_POST['country']."'" ;
	}
	$searchClause = substr($searchClause, 5) ; // remove first instance of " AND"
	$searchClause = " WHERE " . $searchClause ; // prefix with " WHERE "
	$qry = "SELECT * FROM users" . $searchClause ; // suffex the $qry (query) with $searchclause } // end if Search
}
/* ======== PHP PART ========== */

<!– ======== HTML PART ========== –>

<h3>User Search</h3>
<
form name=”testForm” method=”post” action=”somepage.php”>

User Name: <input type=”text” name=”user_name” id=”user_name” size=“20” />
Email: <input type=”text” name=”email” id=”email” size=”35″ />
Country: <select name=”country” id=”country”>
<
option value=””>Select a Country </option>
<option value=”GER”>Germany </option>
<
option value=”KSA”>Saudi Arabia <option>
<
option value=”PAK” selected>Pakistan </option>
<option value=”UK”>United Kingdom </option>
<
option value=”US”>United States </option>
<option value=”OTHER”>Other </option>
</
select>
<
input type=”submit” name=”Search” id=”Search” value=”Search” />
</form>

<!– ======== HTML PART ======== –>

And that’s about it. This form is supposed to submit itself to the same page.

Powered by ScribeFire.

Posted in PHP.

One thought on “Simple User Search in PHP

Leave a comment