search_user.php (2703B) download
1<!DOCTYPE html>
2<html lang="eng">
3 <head>
4 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
5 <link rel="stylesheet" type="text/css" href="/css/homepage.css">
6 <?php
7 include "views/navbar.php";
8 include "utils/autoloader.php";
9 if(!in_array(1, $_SESSION['user_permissions'])){
10 header('Location: /dashboard');
11 exit;
12 }
13 $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "wap2");
14 ?>
15 </head>
16<body>
17 <form class="d-flex" action="search_user" method="post">
18 <input class="form-control me-2" type="search" name="search" placeholder="Email" aria-label="Search">
19 <button class="btn btn-outline-success" type="submit">Search</button>
20</form>
21 <?php
22 if(isset($_POST['search'])) {
23 //set $query
24 $query = "%" . $_POST['search'] . "%";
25 display_results($db, $query);
26 }elseif(isset($_POST['delete'])){
27 $u = $db->get(User::class);
28 $u->where("email", $_POST['delete']);
29 $u->delete();
30 display_results($db, $_POST['query']);
31 }
32 else{
33 display_results($db, '%');
34 }
35 function display_results($db, $query){
36 //create a User orm class and load all the records where user like query
37 $results = $db->all_where(User::class, array('email' => $query));
38
39 // display results
40 if($results != null){
41 echo "<table class=\"table table-striped\">
42 <thead>
43 <tr>
44 <th>E-mail</th>
45 <th>First name</th>
46 <th>Last name</th>
47 <th>Alter</th>
48 <th>Delete</th>
49 </tr>
50 </thead>
51 <tbody>";
52 foreach($results as $data) {
53 $link = "/alter_user?email=" . $data->email;
54 echo "<tr>";
55 echo "<td>" . $data->email . "</td>";
56 echo "<td>" . $data->first_name . "</td>";
57 echo "<td>" . $data->last_name . "</td>";
58 echo "<td><a href='" . $link . "'>Edit</a></td>";
59 echo "
60 <td>
61 <form method=\"post\" action=\"search_user\">
62 <input type=\"hidden\" name=\"query\" value=" . $query. ">
63 <button type=\"submit\" name='delete' value=" . $data->email ." ' class=\"btn btn-primary\">delete</button>
64 </form>
65 </td>";
66 echo "</tr>";
67 }
68 echo"
69 </tbody>
70 </table>";
71 }else{
72 echo "No users with this email address were found.";
73 }
74 }
75 ?>
76 </body>
77</html>
78