-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
51 lines (41 loc) · 1.57 KB
/
functions.php
File metadata and controls
51 lines (41 loc) · 1.57 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
<?php
//returns ALL users in table
function getUsers($sql) {
return $sql->query("
SELECT users.*, roles.name AS role_name
FROM users
JOIN roles ON role_id = roles.id
ORDER BY name DESC")->fetchAll();
}
// selects ONE user, which ID = id (is used for edit / delete or click on detail page)
function getUser($sql, $id) {
$stmt = $sql->prepare("
SELECT * FROM users WHERE id = ?");
$stmt->execute([$id]);
return $stmt->fetch();
}
//(prepares to)insert new data to the table USERS.
//execute -> INSERTS data to table
function addUser($sql, $name, $email, $role_id, $is_active, $avatar) {
$stmt = $sql->prepare("
INSERT INTO users (name, email, role_id, is_active, avatar)
VALUES (?,?,?,?,?)");
$stmt->execute([$name, $email, $role_id, $is_active, $avatar]);
}
//edits values on an ID you click on basically, due to the where id = ?.
function editUser($sql, $id, $name, $email, $role_id, $is_active, $avatar) {
$stmt = $sql->prepare("
UPDATE users
SET name = ?, email = ?, role_id = ?, is_active = ?, avatar = ?
WHERE id = ?");
$stmt->execute([$name, $email, $role_id, $is_active, $avatar, $id]);
}
//deletes row where id = ?.
function deleteUser($sql, $id) {
$stmt = $sql->prepare("DELETE FROM users WHERE id = ?");
$stmt->execute([$id]);
}
//returns all roles
function getRoles($sql){
return $sql->query("SELECT * FROM roles ORDER BY name DESC")->fetchAll();
}