Skip to content

LearnCodeWeb/PHP-CRUD-in-Bootstrap-4-with-search-functionality

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧩 PHP CRUD in Bootstrap 4 with Search Functionality

This project demonstrates how to build a simple CRUD (Create, Read, Update, Delete) system using PHP + MySQL + Bootstrap 4, along with a search feature.


πŸš€ Features

  • Add new users
  • View all users
  • Update user data
  • Delete users
  • Search users by:
    • Name
    • Email
    • Phone
    • Date range

πŸ“ Project Structure

include/
    └── Database.php

add-users.php
browse-users.php
edit-users.php
delete.php
config.php

πŸ› οΈ Database Setup

Create Table

CREATE TABLE `users` (
   `id` INT NOT NULL AUTO_INCREMENT,
   `username` VARCHAR(64) NOT NULL,
   `useremail` VARCHAR(128) NOT NULL,
   `userphone` VARCHAR(24) NOT NULL,
   `dt` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
   PRIMARY KEY (`id`)
);

βš™οΈ config.php

<?php
include_once('include/Database.php');

define('SS_DB_NAME', 'test');
define('SS_DB_USER', 'root');
define('SS_DB_PASSWORD', '');
define('SS_DB_HOST', 'localhost');

$dsn  = "mysql:dbname=".SS_DB_NAME.";host=".SS_DB_HOST;

try {
    $pdo = new PDO($dsn, SS_DB_USER, SS_DB_PASSWORD);
} catch (PDOException $e) {
    die("Connection failed: " . $e->getMessage());
}

$db = new Database($pdo);
?>

βž• Add User (add-users.php)

Form

<form method="post">
    <input type="text" name="username" placeholder="Enter user name" required>
    <input type="email" name="useremail" placeholder="Enter email" required>
    <input type="text" name="userphone" placeholder="Enter phone" required>
    <button type="submit" name="submit">Add User</button>
</form>

Insert Logic

<?php
if(isset($_POST['submit'])){
    $data = [
        'username' => $_POST['username'],
        'useremail' => $_POST['useremail'],
        'userphone' => $_POST['userphone']
    ];

    $db->insert('users', $data);
}
?>

πŸ” Search + Browse (browse-users.php)

Search Logic

<?php
$condition = '';

if(!empty($_GET['username'])){
    $condition .= " AND username LIKE '%".$_GET['username']."%'";
}
if(!empty($_GET['useremail'])){
    $condition .= " AND useremail LIKE '%".$_GET['useremail']."%'";
}
if(!empty($_GET['userphone'])){
    $condition .= " AND userphone LIKE '%".$_GET['userphone']."%'";
}

$userData = $db->getAllRecords('users', '*', $condition);
?>

Search Form

<form method="get">
    <input type="text" name="username" placeholder="Name">
    <input type="email" name="useremail" placeholder="Email">
    <input type="text" name="userphone" placeholder="Phone">
    <button type="submit">Search</button>
</form>

Display Table

<table border="1">
<tr>
    <th>ID</th>
    <th>Name</th>
    <th>Email</th>
    <th>Phone</th>
    <th>Action</th>
</tr>

<?php foreach($userData as $user){ ?>
<tr>
    <td><?= $user['id'] ?></td>
    <td><?= $user['username'] ?></td>
    <td><?= $user['useremail'] ?></td>
    <td><?= $user['userphone'] ?></td>
    <td>
        <a href="edit-users.php?editId=<?= $user['id'] ?>">Edit</a>
        <a href="delete.php?delId=<?= $user['id'] ?>">Delete</a>
    </td>
</tr>
<?php } ?>
</table>

✏️ Edit User (edit-users.php)

<?php
if(isset($_GET['editId'])){
    $user = $db->getAllRecords('users','*'," AND id=".$_GET['editId']);
}

if(isset($_POST['submit'])){
    $data = [
        'username' => $_POST['username'],
        'useremail' => $_POST['useremail'],
        'userphone' => $_POST['userphone']
    ];

    $db->update('users', $data, ['id' => $_POST['editId']]);
}
?>

πŸ—‘οΈ Delete User (delete.php)

<?php
include_once('config.php');

if(isset($_GET['delId'])){
    $db->delete('users', ['id' => $_GET['delId']]);
    header('Location: browse-users.php');
}
?>

πŸ“… Optional: Date Filter (jQuery UI)

<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>

πŸ“¦ Requirements

  • PHP
  • MySQL
  • Bootstrap 4
  • jQuery (optional)

⚠️ Important Notes

  • Use prepared statements (avoid SQL injection)
  • Validate inputs properly
  • Sanitize user data

πŸ“„ License

Free to use and modify.

About

PHP CRUD in Bootstrap 4 with search functionality

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages