-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcc_functions.php
More file actions
151 lines (126 loc) · 3.73 KB
/
cc_functions.php
File metadata and controls
151 lines (126 loc) · 3.73 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
<?PHP
/**
* crime codes related functions;
*/
include 'connect.php';
include 'user.php';
class Crime_code{
public $cc;
public $cd;
public static function fromResultSet($resultSet) {
$result = [];
if ($resultSet) {
foreach ($resultSet as $row) {
$crime_code = Crime_code::fromResultRow($row);
array_push($result, $crime_code);
}
}
return $result;
}
public static function fromResultRow($row) {
$crime_code = new Crime_code();
$crime_code->cc = $row['crime_code'];
$crime_code->cd = $row['code_description'];
return $crime_code;
}
}
function list_crime_code() {
global $con;
$sql = "SELECT * FROM `crime_codes`";
$result = mysqli_query($con, $sql);
return Crime_code::fromResultSet($result);
}
// search
function search_crime_code(){
global $con;
$sql = "select * from crime_codes where ";
$field = $_POST["field"];
$text = $_POST["search_text"];
$equal_fields = ["crime_code", "code_description"];
if (in_array($field, $equal_fields)) {
$sql = $sql . " $field='$text'";
} else {
$sql = $sql . "$field like '%$text%'";
}
$result = mysqli_query($con, $sql);
return Crime_code::fromResultSet($result);
}
//delete
function delete_crime_code() {
User::checkPerm();
global $con;
$cc = $_POST['cc'];
$sql = "DELETE from crime_codes where crime_code=$cc";
$con->begin_transaction();
try {
$con->query($sql);
$con->commit();
} catch (mysqli_sql_exception $exception) {
$con->rollback();
die($exception);
}
}
//get crime_code by cc
function get_crime_code_info_from_db() {
global $con;
$id = array_key_exists('crime_code', $_REQUEST) ? $_REQUEST['crime_code'] : null;
if ($id == null) {
return new Crime_code;
}
$sql = 'select * from crime_codes where crime_code = ' . $id;
try {
$result = $con->query($sql);
if (mysqli_num_rows($result) == 1) {
$row = mysqli_fetch_assoc($result);
return Crime_code::fromResultRow($row);
} else {
die("Crime_code with crime_code $id does not exist.");
}
} catch (mysqli_sql_exception $exception) {
die($exception);
}
}
// insert new
function add_crime_code_info() {
User::checkPerm();
global $con;
$sql = "insert into crime_codes values (?,?)";
$crime_code = Crime_code::fromResultRow($_POST);
try {
$con->begin_transaction();
$stmt = $con->prepare($sql);
$stmt->bind_param("is",
$crime_code->cc,
$crime_code->cd);
$stmt->execute();
$con->commit();
header("location:cc.php");
} catch (mysqli_sql_exception $exception) {
$con->rollback();
die($exception);
}
}
//update crime_code
function update_crime_code_info() {
User::checkPerm();
global $con;
$sql = "UPDATE `crime_codes` SET `crime_code`=?,
`code_description`= ?
WHERE crime_code = ?";
$crime_code = Crime_code::fromResultRow($_POST);
try {
$con->begin_transaction();
$stmt = $con->prepare($sql);
$stmt->bind_param("isi",
$crime_code->cc,
$crime_code->cd,
$crime_code->cc);
$stmt->execute();
$con->commit();
header("location:cc_add_and_edit.php?m=e&success=t&crime_code=$crime_code->cc");
} catch (mysqli_sql_exception $exception) {
$con->rollback();
die($exception);
}
}
?>