FAUSTINE MWOYA November 20, 2025

Jinsi ya Kuongeza Search Option kwenye User Management Dashboard kwa PHP

Jifunze jinsi ya kuongeza search functionality kwenye User Management Dashboard kwa PHP. Mtumiaji anaweza kutafuta watumiaji kwa username au email ili kurahisisha usimamizi wa watumiaji. Tutorial hii inatoa working example rahisi ya search option.

Example Code:
1. Dashboard with Search
<?php
session_start();
if (!isset($_SESSION['admin_logged']) || $_SESSION['admin_logged'] !== true) {
header("Location: login.php");
exit;
}

// Database connection
$pdo = new PDO("mysql:host=localhost;dbname=test_db;charset=utf8", "root", "");

// Handle search
$search = $_GET['search'] ?? '';
if ($search) {
$stmt = $pdo->prepare("SELECT * FROM users WHERE username LIKE ? OR email LIKE ?");
$stmt->execute(["%$search%", "%$search%"]);
} else {
$stmt = $pdo->query("SELECT * FROM users");
}
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<h1>User Management Dashboard</h1>
<a href="logout.php">Logout</a>

<!-- Search Form -->
<form method="GET">
<input type="text" name="search" placeholder="Search by username or email" value="<?= htmlspecialchars($search) ?>">
<button type="submit">Search</button>
</form>

<h2>All Users</h2>
<table border="1" cellpadding="5">
<tr>
<th>ID</th>
<th>Username</th>
<th>Email</th>
<th>Actions</th>
</tr>
<?php foreach ($users as $user): ?>
<tr>
<td><?= $user['id'] ?></td>
<td><?= htmlspecialchars($user['username']) ?></td>
<td><?= htmlspecialchars($user['email']) ?></td>
<td>
<a href="edit_user.php?id=<?= $user['id'] ?>">Edit</a> |
<a href="dashboard.php?delete=<?= $user['id'] ?>" onclick="return confirm('Delete?')">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>

2. Logout (logout.php)
<?php
session_start();
session_destroy();
header("Location: login.php");
exit;
?>

Links Za Kujifunza Zaidi

🌐 Faulink Official Website:
https://www.faulink.com/

📘 Jifunze Web Design & Programming:
https://www.faulink.com/excel_mifumo.php

📲 Piga / WhatsApp kwa msaada wa haraka:
https://wa.me/255693118509
Share this post
Previous Next

Comments

0
No comments yet. Be the first to comment.

Continue Reading

Subscribe

Get new updates

Jiunge upokee posts mpya, tutorials, na updates za mifumo moja kwa moja kwenye email yako.

Chat na Faulink