-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolice_functions.php
More file actions
177 lines (151 loc) · 4.71 KB
/
police_functions.php
File metadata and controls
177 lines (151 loc) · 4.71 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?PHP
/**
* police related functions;
*/
include 'connect.php';
include 'user.php';
class Police{
public $id;
public $fname;
public $lname;
public $precinct;
public $badge;
public $phone;
public $status;
public static function fromResultSet($resultSet) {
$result = [];
if ($resultSet) {
foreach ($resultSet as $row) {
$police = Police::fromResultRow($row);
array_push($result, $police);
}
}
return $result;
}
public static function fromResultRow($row) {
$police = new Police();
$police->id = $row['officer_ID'];
$police->fname = $row['officer_name_first'];
$police->lname = $row['officer_name_last'];
$police->precinct = $row['officer_precinct'];
$police->badge = $row['officer_badge'];
$police->phone = $row['officer_phone'];
$police->status = $row['officer_status'];
return $police;
}
}
function list_police() {
global $con;
$sql = "SELECT * FROM `officers`";
$result = mysqli_query($con, $sql);
return Police::fromResultSet($result);
}
// search
function search_police() {
global $con;
$sql = "select * from officers where ";
$field = $_POST["field"];
$text = $_POST["search_text"];
$equal_fields = ["officer_ID", "officer_badge", "officer_phone", "officer_precinct"];
if (in_array($field, $equal_fields)) {
$sql = $sql . " $field='$text'";
} else {
$sql = $sql . "$field like '%$text%'";
}
$result = mysqli_query($con, $sql);
return Police::fromResultSet($result);
}
//delete
function delete_police() {
User::checkPerm();
global $con;
$id = $_POST['id'];
$sql = "DELETE from officers where officer_ID=$id";
$con->begin_transaction();
try {
$con->query($sql);
$con->commit();
} catch (mysqli_sql_exception $exception) {
$con->rollback();
die($exception);
}
}
//get police by id
function get_police_info_from_db() {
global $con;
$id = array_key_exists('officer_ID', $_REQUEST) ? $_REQUEST['officer_ID'] : null;
if ($id == null) {
return new Police;
}
$sql = 'select * from officers where officer_ID = ' . $id;
try {
$result = $con->query($sql);
if (mysqli_num_rows($result) == 1) {
$row = mysqli_fetch_assoc($result);
return Police::fromResultRow($row);
} else {
die("Police with id $id does not exist.");
}
} catch (mysqli_sql_exception $exception) {
die($exception);
}
}
// insert new
function add_police_info() {
User::checkPerm();
global $con;
$sql = "insert into officers values (?,?,?,?,?,?,?)";
$police = Police::fromResultRow($_POST);
try {
$con->begin_transaction();
$stmt = $con->prepare($sql);
$stmt->bind_param("issssss",
$police->id,
$police->fname,
$police->lname,
$police->precinct,
$police->badge,
$police->phone,
$police->status);
$stmt->execute();
$con->commit();
header("location:police.php");
} catch (mysqli_sql_exception $exception) {
$con->rollback();
die($exception);
}
}
//update police
function update_police_info() {
User::checkPerm();
global $con;
$sql = "UPDATE `officers` SET `officer_ID`=?,
`officer_name_first`= ?,
`officer_name_last`= ?,
`officer_precinct`= ?,
`officer_badge`= ?,
`officer_phone`= ?,
`officer_status`= ?
WHERE officer_ID = ?";
$police = Police::fromResultRow($_POST);
try {
$con->begin_transaction();
$stmt = $con->prepare($sql);
$stmt->bind_param("issssssi",
$police->id,
$police->fname,
$police->lname,
$police->precinct,
$police->badge,
$police->phone,
$police->status,
$police->id);
$stmt->execute();
$con->commit();
header("location:police_add_and_edit.php?m=e&success=t&officer_ID=$police->id");
} catch (mysqli_sql_exception $exception) {
$con->rollback();
die($exception);
}
}
?>