Jump to: navigation, search

PHP Search Form

From w3cyberlearnings

Contents

Table: registration table

  • Table uses for this search form.
CREATE TABLE `register` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(40) DEFAULT NULL,
  `middle_name` varchar(40) DEFAULT NULL,
  `last_name` varchar(40) DEFAULT NULL,
  `phone` varchar(10) DEFAULT NULL,
  `login_name` varchar(40) DEFAULT NULL,
  `password` varchar(40) DEFAULT NULL,
  PRIMARY KEY (`id`)
) 

search.php

<?php
$mysqli_db = new mysqli('localhost', 'root', 'yeething', 'my_db');

// normal search
$result_tb = "";
if (!empty($_POST['SEARCH'])) {

$e = $_POST['search_value'];

$query = 'SELECT * FROM register WHERE ' .
		  "first_name LIKE '%$e%' OR " .
		  "middle_name LIKE '%$e%' OR " .
		  "last_name LIKE '%$e%' OR " .
		  "login_name LIKE '%$e%' ";

$query_result = $mysqli_db->query($query);

	$result_tb = '<table cellspacing="5" cellpadding="5">';
	while ($rows = $query_result->fetch_assoc()) {
		foreach ($rows as $k => $v) {
			$result_tb .= '<tr><td>' . $k . '</td><td>' . $v . '</td></tr>';
		}
	}
	$result_tb .='</table>';

$mysqli_db->close();
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Search</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
	<table>
		<tr>
			<td>
				<input type="text" name="search_value" size="30" maxlength="30"/>
			</td>
			<td>
				<input type="submit" name="SEARCH" value="Search"/>
			</td>
		</tr>
	</table>
</form>
<?php echo $result_tb; ?>
</body>
</html>

Output

Php form search 1.png

Related Links


HTML Form
  1. PHP Login Form
  2. PHP Feedback Form
  3. PHP Register Form
  4. PHP Search Form
  5. PHP Image Upload and MySQL
  6. PHP Multiple Forms
  7. PHP Generate Form by Ajax
Navigation
Web
SQL
MISC
References