Search a Record from database in PHP MySQL
Search a Record from database in PHP MySQL
Let’s start the code of “Search a Record from database in PHP MYsql”.
File 1: Index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
<!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> <div class="col-md-3"></div> <div class="col-md-6 well" style="width: 95% !important;left: 40px;position: absolute; padding: 40px; padding-bottom: 100px; background-color: lightgrey;"> <h3 style="color: green" class="text-primary">Search Box - PHP</h3> <hr style="border-top:2px dashed #ccc;"/> <div class="col-md-1"></div> <div class="col-md-10"> <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" name="keyword" required="required" style="border: 2px; padding: 20px;" /> <span class="input-group-btn"> <button class="btn btn-primary" name="search" style="background-color: green !important;border-color: green; " ><span class="glyphicon glyphicon-search"></span></button> </span> </div> </form> <br /> <br/> <br/> <center><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal" style="background-color: green !important;border-color: green; padding-left:20px; padding-right:20px; padding-top: 5px; padding-bottom: 5px;">Insert data</button></center> <?php include 'find.php'?> <a style="color: green; text-decoration: none;" href="https://t4tutorials.com/"><h3>Visit T4Tutorials.com</h3></a> </div> </div> <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true"> <div class="modal-dialog"> <form action="insert_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>Subject</label> <input type="text" class="form-control" name="title" required="required"/> </div> <div class="form-group"> <label>Data</label> <textarea class="form-control" style="resize:none; height:250px;" name="data" required="required"></textarea> </div> </div> </div> <div style="clear:both;"></div> <div class="modal-footer"> <center> <button name="save" class="btn btn-primary" style="background-color: green !important; border-color: green; padding-left:20px; padding-right:20px; padding-top: 5px; padding-bottom: 5px;"><span class="glyphicon glyphicon-save"></span> Insert</button> <button type="button" class="btn btn-danger"
style="background-color: red !important; border-color: red; padding-left:20px; padding-right:20px; padding-top: 5px; padding-bottom: 5px;" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Exit</button> </center> </div> </div> </form> </div> </div> <script src="js/jquery-3.2.1.min.js"></script> <script src="js/bootstrap.js"></script> </body> </html> |
File 2: Find.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php if(ISSET($_POST['search'])){ $keyword = $_POST['keyword']; ?> <div> <h2>Result</h2> <hr style="border-top:2px dotted #ccc;"/> <?php require 'connection.php'; $query = mysqli_query($conn, "SELECT * FROM `posts` WHERE `subject` LIKE '%$keyword%' ORDER BY `subject`") or die(mysqli_error()); while($fetch = mysqli_fetch_array($query)){ ?> <div style="word-wrap:break-word;"> <a href="find_post.php?id=<?php echo $fetch['post_id']?>"><h4><?php echo $fetch['subject']?></h4></a> <p><?php echo substr($fetch['data'], 0, 100)?>...</p> </div> <hr style="border-bottom:1px solid #ccc;"/> <?php } ?> </div> <?php } ?> |
File 3: Find_post.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<!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> <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 'connection.php'; if(ISSET($_REQUEST['id'])){ $query = mysqli_query($conn, "SELECT * FROM `posts` WHERE `post_id` = '$_REQUEST[id]'") or die(mysqli_error()); $fetch = mysqli_fetch_array($query); ?> <h3><?php echo $fetch['subject']?></h3> <p><?php echo nl2br($fetch['data'])?></p> <?php } ?> </div> </body> </html> |
Insert_content.php
1 2 3 4 5 6 7 8 9 10 |
<?php require_once 'connection.php'; if(ISSET($_POST['save'])){ $subject = addslashes($_POST['subject']); $data = addslashes($_POST['data']); mysqli_query($conn, "INSERT INTO `posts` VALUES('', '$subject', '$data')") or die(mysqli_error()); header('location: index.php'); } ?> |
Database for Search a Record from database in PHP MySQL
Download – How to search a record from database in php mysql