Create a Simple Search Box

Create a Simple Search Box in PHP MySQL

Create a Simple Search Box in PHP MySQL Free Download, , download nulled php script, php script for free, download nulled source code for php, learn php, learn php

PHP Summary: Create a Simple Search Box in PHP

How to create a Simple Search Box with PHP and MySQL. PHP is a server-side scripting language created primarily for web development. PHP enables direct user interaction with the script and makes it simple for users to grasp its syntax. Because of its user-friendly environment, it is primarily utilized by beginning programmers.

Getting Started:

You must first download and install XAMPP or another local server capable of running PHP scripts. The XAMPP server connection is provided here

And this is the URL for the jQuery library utilized for this lesson.

Finally, here is a link to the bootstrap framework utilized to create the layout.   

Creating Database

Create a database there with the name “db_box” by opening your database web server. Once you’ve found the database file inside the application’s folder, click OK after clicking Import.

You could alternatively copy and paste the code below in the SQL tab in PHPMyAdmin to create the example table and insert the example data.

				
					CREATE TABLE `blog` (
  `blog_id` int(11) NOT NULL PRIMARY KEY AUTO_INCRMENT,
  `title` varchar(50) NOT NULL,
  `content` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
INSERT INTO `blog` (`blog_id`, `title`, `content`) VALUES
(1, 'The Little Gingerbread Man', 'Once upon a time there was an old woman who loved baking gingerbread. She would bake gingerbread cookies, cakes, houses and gingerbread people, all decorated with chocolate and peppermint, caramel candies and colored frosting.'),
(2, 'The Halloween House', 'Now Suzie\'s moved in--she\'s only 4--along with her brother, her father and mother, and little Picador. He\'s their dog. Well, maybe half a dog. He\'s a Chihuahua, as small as they come.\r\n\r\nSuzie\'s room is in the attic. It\'s no fun. With a high ceiling, cold and gloomy,
				
			

Creating the database connection

Launch your chosen text editor, such as Sublime Text 3, Notepad ++, or another. Create a new conn.php file and copy and paste the code below.

				
					<?php
	$conn = mysqli_connect('localhost', 'root', '', 'db_box') or die(mysqli_error());
 
	if(!$conn){
		die("Error: Failed to connect to database");
	}
?>
				
			

Creating The Interface

Here, we’ll design a straightforward application form. Copy and paste the forms into your text editor to write them, then save them as index.php.

				
					<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" name="viewport" content="width=device-width"/>
		<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
	</head>
<body>
	<nav class="navbar navbar-default">
		<div class="container-fluid">
			<a class="navbar-brand" href="https://campcodes.com">CampCodes</a>
		</div>
	</nav>
	<div class="col-md-3"></div>
	<div class="col-md-6 well">
		<h3 class="text-primary">PHP - Simple Search Box</h3>
		<hr style="border-top:1px dotted #ccc;"/>
		<div class="col-md-1"></div>
		<div class="col-md-10">
			<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal">Add Content</button>
			<br />
			<br />
			<form class="form-inline" method="POST" action="index.php">
				<div class="input-group col-md-12">
					<input type="text" class="form-control" placeholder="Search here..." name="keyword" required="required" value="<?php echo isset($_POST['keyword']) ? $_POST['keyword'] : '' ?>"/>
					<span class="input-group-btn">
						<button class="btn btn-primary" name="search"><span class="glyphicon glyphicon-search"></span></button>
					</span>
				</div>
			</form>
			<br />
			<?php
				if(ISSET($_POST['search'])){
					$keyword = $_POST['keyword'];
			?>
			<div>
				<h2>Result</h2>
				<hr style="border-top:2px dotted #ccc;"/>
				<?php
					require 'conn.php';
					$query = mysqli_query($conn, "SELECT * FROM `blog` WHERE `title` LIKE '%$keyword%' ORDER BY `title`") or die(mysqli_error());
					while($fetch = mysqli_fetch_array($query)){
				?>
				<div style="word-wrap:break-word;">
					<a href="get_blog.php?id=<?php echo $fetch['blog_id']?>"><h4><?php echo $fetch['title']?></h4></a>
					<p><?php echo substr($fetch['content'], 0, 100)?>...</p><div class='code-block code-block-1' style='margin: 8px auto; text-align: center; display: block; clear: both;'>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7019695460453069"
     crossorigin="anonymous"></script>
<!-- First Point Ads -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-7019695460453069"
     data-ad-slot="2810488486"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></div>

				</div>
				<hr style="border-bottom:1px solid #ccc;"/>
				<?php
					}
				?>
			</div>
			<?php
				}
			?>
		</div>
	</div>
	<div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
		<div class="modal-dialog">
			<form action="save_content.php" method="POST" enctype="multipart/form-data">
				<div class="modal-content">
					<div class="modal-body">
						<div class="col-md-2"></div>
						<div class="col-md-8">
							<div class="form-group">
								<label>Title</label>
								<input type="text" class="form-control" name="title" required="required"/>
							</div>
							<div class="form-group">
								<label>Content</label>
								<textarea class="form-control" style="resize:none; height:250px;" name="content" required="required"></textarea>
							</div>
						</div>
					</div>
					<div style="clear:both;"></div>
					<div class="modal-footer">
						<button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
						<button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
					</div>
				</div>
			</form>
		</div>
	</div>
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.js"></script>
</body>
</html>
				
			

Creating Save Query

The application’s save query is contained in this code. The data will be saved to the database server using this code. Simply paste this piece of code into a text editor to accomplish this, then save it as save_content.php.

				
					<?php
	require_once 'conn.php';
 
	if(ISSET($_POST['save'])){
		$title = addslashes($_POST['title']);
		$content = addslashes($_POST['content']);
 
		mysqli_query($conn, "INSERT INTO `blog` VALUES('', '$title', '$content')") or die(mysqli_error());
 
		header('location: index.php');
 
	}
?>
				
			

Creating the View Content

The application’s view content is contained in this code. The inputted search keyword text will be displayed by this script. Simply copy the code below into a text editor, and save it as get_blog.php.

				
					<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" name="viewport" content="width=device-width"/>
		<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
	</head>
<body>
	<nav class="navbar navbar-default">
		<div class="container-fluid">
			<a class="navbar-brand" href="https://campcodes.com">CampCodes</a>
		</div>
	</nav>
	<div class="col-md-3"></div>
	<div class="col-md-6 well">
		<h3 class="text-primary">PHP - Simple Search Box</h3>
		<hr style="border-top:1px dotted #ccc;"/>
		<a href="index.php" class="btn btn-success">Back</a>
		<?php
			require 'conn.php';
			if(ISSET($_REQUEST['id'])){
				$query = mysqli_query($conn, "SELECT * FROM `blog` WHERE `blog_id` = '$_REQUEST[id]'") or die(mysqli_error());
				$fetch = mysqli_fetch_array($query);
		?>
				<h3><?php echo $fetch['title']?></h3>
				<p><?php echo nl2br($fetch['content'])?></p>
		<?php
			}
		?>
 
	</div>
</body>
</html>
				
			

Creating the Main Function

The primary functionality of the application is contained in this code. This code will locate and look up the entered term to see if it is already present in the database. To do this, simply copy and paste the code into a text editor, then save it as search.php.

				
					<?php
	if(ISSET($_POST['search'])){
		$keyword = $_POST['keyword'];
?>
<div>
	<h2>Result</h2>
	<hr style="border-top:2px dotted #ccc;"/>
	<?php
		require 'conn.php';
		$query = mysqli_query($conn, "SELECT * FROM `blog` WHERE `title` LIKE '%$keyword%' ORDER BY `title`") or die(mysqli_error());
		while($fetch = mysqli_fetch_array($query)){
	?>
	<div style="word-wrap:break-word;">
		<a href="get_blog.php?id=<?php echo $fetch['blog_id']?>"><h4><?php echo $fetch['title']?></h4></a>
		<p><?php echo substr($fetch['content'], 0, 100)?>...</p>
	</div>
	<hr style="border-bottom:1px solid #ccc;"/>
	<?php
		}
	?>
</div>
<?php
	}
?>
				
			

So there you have it—using PHP, we were able to effectively develop a Simple Search Box. I sincerely hope that this straightforward instruction enables you to find what you need. Please visit this website for further information and tutorials.

Output

Don’t forget to share this post!

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Article