forked from ohmiler/pdo-oop-crud-modal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.php
76 lines (66 loc) · 2.57 KB
/
action.php
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
<?php
require_once "db.php";
require_once "util.php";
$db = new Database();
$util = new Util();
if (isset($_POST['add'])) {
$fname = $util->testInput($_POST['fname']);
$lname = $util->testInput($_POST['lname']);
$email = $util->testInput($_POST['email']);
$phone = $util->testInput($_POST['phone']);
if ($db->insert($fname, $lname, $email, $phone)) {
echo $util->showMessage("success", "User inserted successfully!");
} else {
echo $util->showMessage("danger", "Something went wrong!");
}
}
if (isset($_GET['read'])) {
$users = $db->read();
$output = '';
if ($users) {
foreach($users as $row) {
$output .= '<tr>
<td>' . $row['id'] . '</td>
<td>' . $row['first_name'] . '</td>
<td>' . $row['last_name'] . '</td>
<td>' . $row['email'] . '</td>
<td>' . $row['phone'] . '</td>
<td>
<a href="#" id="'. $row['id'] .'" class="btn btn-success btn-sm rounded-pull py-0 editlink" data-bs-toggle="modal" data-bs-target="#editUserModal">Edit</a>
<a href="#" id="'. $row['id'] .'" class="btn btn-danger btn-sm rounded-pull py-0 deletelink">Delete</a>
</td>
</tr>';
}
echo $output;
} else {
echo '<tr>
<td colspan="6">No users found in the Database</td>
</tr>';
}
}
if (isset($_GET['edit'])) {
$id = $_GET['id'];
$user = $db->readOne($id);
echo json_encode($user);
}
if (isset($_POST['update'])) {
$id = $util->testInput($_POST['id']);
$fname = $util->testInput($_POST['fname']);
$lname = $util->testInput($_POST['lname']);
$email = $util->testInput($_POST['email']);
$phone = $util->testInput($_POST['phone']);
if ($db->update($id, $fname, $lname, $email, $phone)) {
echo $util->showMessage("success", "User updated successfully!");
} else {
echo $util->showMessage("danger", "Something went wrong!");
}
}
if (isset($_GET['delete'])) {
$id = $_GET['id'];
if ($db->delete($id)) {
echo $util->showMessage("info", "User deleted successfully!");
} else {
echo $util->showMessage("danger", "Something went wrong!");
}
}
?>