In this Blog, You Can Have The Complete Source Code Of Responsive CRUD Application With Search Functionality With Bootstrap 4 Developed In PHP & MySQL.

Main Output

First Create The Database And Table In PhpMyAdmin

Employees Table Structure

Table Data

Database Connection Code

<?php
$con = mysqli_connect('localhost','root','','legends_db');
    
    if(!$con)
    {
        echo "Connection Failed";
    }
    
//    if($con)
//    {
//        
//        echo "Connection Established..";
//    }
//    else
//    {
//        echo "Connection Failed..";
//    }
?>

Add Employee Form

Insert Form Code

<?php
include('include/dbconnection.php');

?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
    
    <link rel="stylesheet" href="css/bootstrap.min.css">
</head>

<body>
    
    
    <div class="container">
    
        <div class="row">
        
            <div class="col-md-4 mx-auto">
            
                <h1 class="jumbotron text-center bg-primary">Add Employee</h1>
                
                <form action="" method="post">
    
        <input type="text" class="form-control" name="ename" id="" placeholder="Enter Name">
                    <br>
        <input type="number" class="form-control" min="10" max="60" name="eage" id="" placeholder="Enter Age">
                    <br>
        <input type="text" class="form-control" name="edesig" id="" placeholder="Enter Designation">
                    <br>
        <input type="text" class="form-control" name="esalary" id="" placeholder="Enter Salary">
                    <br>
                    
        <input type="submit" value="Insert" name="InsertBtn" class="btn btn-primary btn-block">
        </form>
                
            </div>
        
        </div>
    
    </div>
    
    <script src="js/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
</body>
</html>

<?php


if(isset($_POST['InsertBtn']))
{
    
    $name = $_POST['ename'];
    $age = $_POST['eage'];
    $desig = $_POST['edesig'];
    $salary = $_POST['esalary'];
    
    
    $query = "insert into employees (name, age, designation, salary) values('$name','$age','$desig','$salary')";
    
    $run = mysqli_query($con, $query);
    
    if($run)
    {
        echo "<script> alert('Data has been inserted Successfully..'); 
        window.location.href = 'Read.php';
        </script>";
        
    }
    else
    {
        echo "<script> alert('Data insertion Failed..'); </script>";
    }
    
}

?>

Read Data From Employees Table In HTML Table

Fetch / Read Data From Database Table – Source Code

<?php
include('include/dbconnection.php');

$query = "select * from employees";

if(isset($_GET['SearchBtn']))
{
    $searchBy = $_GET['SearchList'];
    $search_text = $_GET['SearchTxt'];
    
    if($searchBy == "Id")
    {
     $query = "select * from employees where id = '$search_text'";   
    }
    else if($searchBy == "Name")
    {
     $query = "SELECT * from employees WHERE name LIKE '%$search_text%'";   
    }
    else if($searchBy == "Designation")
    {
     $query = "select * from employees where designation = '$search_text'";   
    }
    else
    {
        echo "<script>alert('No Search Result Found.');</script>";
    }
    
    
}

$rows = mysqli_query($con, $query);
$Total_Rows = mysqli_num_rows($rows);
//echo $Total_Rows;
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
</head>

<body>
    
  
    <div class="container">
    
        
        <div class="row mt-3">
        
            <div class="col-md-3">
            
            <a href="Home.php" class="btn btn-outline-dark">Add New Employee</a>
            
            </div>
            
            <div class="col-md-8">
                
                <form action="" method="get" class="form-inline">
                           
                    <select name="SearchList" id="" required class="form-control">
                        <option value="">Select Field</option>
                        <option value="Id">Id</option>
                        <option value="Name">Name</option>
                        <option value="Designation">Designation</option>
                    </select>
                    <input type="text" placeholder="Search By " name="SearchTxt" required class="form-control">
                    
                    <input type="submit" value="Search" name="SearchBtn" class="btn btn-primary">
                    <a href="Read.php" class="btn btn-danger">Reset</a>
                        
                    
                </form>
            
            </div>
        
        </div>
        
        <div class="row mt-3">
        
            <div class="col-md-12">
    
    <?php
    
if($Total_Rows != 0)
{
    ?>
    
    <table class="table table-bordered table-striped table-hover table-dark">
        <thead>
        <tr>
            <th colspan="7" class="text-center">EMPLOYEE'S DATA</th>
        </tr>
        <tr>
            <th>ID</th>
            <th>NAME</th>
            <th>AGE</th>
            <th>DESIGNATION</th>
            <th>SALARY</th>
            <th>UPDATE</th>
            <th>DELETE</th>
        </tr>
    </thead>
    <?php
    echo "<tbody>";
    while($data = mysqli_fetch_assoc($rows))
    {
        echo "<tr>
            <td>".$data['id']."</td>
            <td>".$data['name']."</td>
            <td>".$data['age']."</td>
            <td>".$data['designation']."</td>
            <td>".$data['salary']."</td>
            <td><a href='update.php?id=$data[id]' class='btn btn-info'>Edit</a></td>
            <td><a href='delete.php?id=$data[id]' class='btn btn-danger' onclick='return Confirmation()'>Delete</a></td>
        </tr>";
    }
    echo "</tbody>";
}
else
{
    echo "No Rows Found";
}

    
    ?>
    </table>
                </div>
        
        </div>
    
    </div>
    
    <script>
    
    function Confirmation()
        {
            return confirm("Are You Sure To Delete Data ??");
        }
    
    </script>
    
    <script src="js/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
</body>
</html>

When You Click The Edit Button From the Read Page

Update Page Source Code

<?php
include('include/dbconnection.php');

$id = $_GET["id"]??"";

$query = "select * from employees where id = '$id'";
$row = mysqli_query($con, $query);
$data = mysqli_fetch_array($row);

?>

<!--null coalasce operator ??-->

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
    
    <div class="container">
    
        <div class="row">
        
            <div class="col-md-4 mx-auto">
            
                <h1 class="jumbotron text-center bg-primary">Update Employee</h1>
                
                <form action="" method="post">
    
                    <input type="hidden" name="eid" value="<?php  echo $data[0]; ?>">
                    
        <input type="text" class="form-control" name="ename" id="" placeholder="Enter Name" value="<?php  echo $data[1]; ?>">
                    <br>
        <input type="number" class="form-control" min="10" max="60" name="eage" id="" placeholder="Enter Age" value="<?php  echo $data[2]; ?>">
                    <br>
        <input type="text" class="form-control" name="edesig" id="" placeholder="Enter Designation" value="<?php  echo $data[3]; ?>">
                    <br>
        <input type="text" class="form-control" name="esalary" id="" placeholder="Enter Salary" value="<?php echo $data[4]; ?>">
                    <br>
                    
        <input type="submit" value="Update" name="UpdateBtn" class="btn btn-primary btn-block">
        </form>
                
            </div>
        
        </div>
    
    </div>
    
    
      <script src="js/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
</body>
</html>


<?php

if(isset($_POST['UpdateBtn']))
{
    $id = $_POST['eid'];
    $name = $_POST['ename'];
    $age = $_POST['eage'];
    $desig = $_POST['edesig'];
    $salary = $_POST['esalary'];
    
    $query = "UPDATE employees set name = '$name', age = '$age', designation = '$desig', salary = '$salary' where id = '$id'";
    
    $run = mysqli_query($con, $query);
    if($run)
    {
        echo "<script> alert('Data has been Updated Successfully..'); 
        window.location.href = 'Read.php';
        </script>";
        
    }
    else
    {
        echo "<script> alert('Data Updation Failed..'); </script>";
    }

 
}

?>

Delete Button Source Code

When you press the delete button there will be a confirm dialog box appears on the screen.

Delete Page Source Code

<?php
include('include/dbconnection.php');

$id = $_GET["id"]??"";

$query = "delete from employees where id = '$id'";
$run = mysqli_query($con, $query);

if($run)
    {
        echo "<script> alert('Data has been Deleted Successfully..'); 
        window.location.href = 'Read.php';
        </script>";
        
    }
    else
    {
        echo "<script> alert('Data Deletion Failed..'); </script>";
    }
?>

Click Below Link to Download Source Code Of This Blog

https://www.mediafire.com/file/tfjuxyaf70c5h87/crud_php.rar/file

No responses yet

Leave a Reply

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