-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_users.php
More file actions
121 lines (114 loc) · 3.36 KB
/
view_users.php
File metadata and controls
121 lines (114 loc) · 3.36 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
require_once 'config/database.php';
// Load environment variables
loadEnv();
try {
$conn = getDBConnection();
// Handle Delete Request
if (isset($_GET['delete'])) {
$id = $_GET['delete'];
$sql = "DELETE FROM users WHERE id = :id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->execute();
header("Location: view_users.php");
exit();
}
// Fetch all users
$stmt = $conn->query("SELECT * FROM users ORDER BY created_at DESC");
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>View Users</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1000px;
margin: 20px auto;
padding: 20px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
padding: 10px;
border: 1px solid #ddd;
text-align: left;
}
th {
background-color: #4CAF50;
color: white;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
.action-btn {
padding: 5px 10px;
margin: 0 5px;
border: none;
border-radius: 3px;
cursor: pointer;
}
.edit-btn {
background-color: #2196F3;
color: white;
}
.delete-btn {
background-color: #f44336;
color: white;
}
.add-btn {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
text-decoration: none;
display: inline-block;
border-radius: 4px;
}
</style>
</head>
<body>
<h2>User List</h2>
<a href="index.html" class="add-btn">Add New User</a>
<table>
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone</th>
<th>Created At</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?php echo htmlspecialchars($user['id']); ?></td>
<td><?php echo htmlspecialchars($user['first_name']); ?></td>
<td><?php echo htmlspecialchars($user['last_name']); ?></td>
<td><?php echo htmlspecialchars($user['email']); ?></td>
<td><?php echo htmlspecialchars($user['phone']); ?></td>
<td><?php echo htmlspecialchars($user['created_at']); ?></td>
<td>
<a href="edit_user.php?id=<?php echo $user['id']; ?>" class="action-btn edit-btn">Edit</a>
<a href="view_users.php?delete=<?php echo $user['id']; ?>"
class="action-btn delete-btn"
onclick="return confirm('Are you sure you want to delete this user?')">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>