This project demonstrates how to build a simple CRUD (Create, Read, Update, Delete) system using PHP + MySQL + Bootstrap 4, along with a search feature.
- Add new users
- View all users
- Update user data
- Delete users
- Search users by:
- Name
- Phone
- Date range
include/
βββ Database.php
add-users.php
browse-users.php
edit-users.php
delete.php
config.php
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`)
);<?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);
?><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><?php
if(isset($_POST['submit'])){
$data = [
'username' => $_POST['username'],
'useremail' => $_POST['useremail'],
'userphone' => $_POST['userphone']
];
$db->insert('users', $data);
}
?><?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);
?><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><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><?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']]);
}
?><?php
include_once('config.php');
if(isset($_GET['delId'])){
$db->delete('users', ['id' => $_GET['delId']]);
header('Location: browse-users.php');
}
?><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>- PHP
- MySQL
- Bootstrap 4
- jQuery (optional)
- Use prepared statements (avoid SQL injection)
- Validate inputs properly
- Sanitize user data
Free to use and modify.