-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb-admin.php
More file actions
273 lines (271 loc) · 9.87 KB
/
Copy pathdb-admin.php
File metadata and controls
273 lines (271 loc) · 9.87 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
function is_valid_admin($login, $pw) {
global $db;
$query = "SELECT staff_id FROM staff WHERE login = :login AND pw = :pw";
try {
$statement = $db->prepare($query);
$statement->bindValue(':login', $login);
$statement->bindValue(':pw', $pw);
$statement->execute();
$valid = ($statement->rowCount() == 1);
$statement->closeCursor();
return $valid;
} catch (PDOException $e) {
$error = $e->getMessage();
$error_message = "<p>Error validating credentials: $error </p>";
include 'error.php';
exit();
}
}
function get_voters() {
global $db;
$query = "SELECT first_name, last_name, email, voter_id FROM mail";
try {
$statement = $db->prepare($query);
$statement->execute();
$voters = $statement->fetchAll();
$statement->closeCursor();
return $voters;
} catch (PDOException $e) {
$error = $e->getMessage();
$error_message = "<p>Error running database query: $error </p>";
include 'error.php';
exit();
}
}
function get_voter($voter_id) {
global $db;
$query = "SELECT * FROM mail WHERE voter_id = :voter_id";
try {
$statement = $db->prepare($query);
$statement->bindValue(':voter_id', $voter_id);
$statement->execute();
$voter = $statement->fetch();
$statement->closeCursor();
return $voter;
} catch (PDOException $e) {
$error = $e->getMessage();
$error_message = "<p>Error running database query: $error </p>";
include 'error.php';
exit();
}
}
function get_donations() {
global $db;
$query = "SELECT CONCAT(first_name, ' ', last_name) AS name,
donation_date, donation_amount
FROM donations JOIN mail USING(voter_id)
ORDER BY donation_date";
try {
$statement = $db->prepare($query);
$statement->execute();
$donations = $statement->fetchAll();
$statement->closeCursor();
return $donations;
} catch (PDOException $e) {
$error = $e->getMessage();
$error_message = "<p>Error adding new voter: $error </p>";
include 'error.php';
exit();
}
}
//******* staffs vs staff **********************
// get_staffs() is for ALL staff members
function get_staffs() {
global $db;
$query = "SELECT first_name, last_name, email, phone, login, staff_id "
. "FROM staff ORDER BY staff_id";
try {
$statement = $db->prepare($query);
$statement->execute();
$staffs = $statement->fetchAll();
$statement->closeCursor();
return $staffs;
} catch (PDOException $e) {
$error = $e->getMessage();
$error_message = "<p>Error running database query: $error </p>";
include 'error.php';
exit();
}
}
// get_staff() is for INDIVIDUAL staff members
function get_staff($staff_id) {
global $db;
$query = "SELECT * FROM staff WHERE staff_id = :staff_id";
try {
$statement = $db->prepare($query);
$statement->bindValue(':staff_id', $staff_id);
$statement->execute();
$staff = $statement->fetch();
$statement->closeCursor();
return $staff;
} catch (PDOException $e) {
$error = $e->getMessage();
$error_message = "<p>Error running database query: $error </p>";
include 'error.php';
exit();
}
}
/*****
add_staff will be used for adding new staff by admin
and when voters complete the volunteer form
******/
function add_staff($first_name, $last_name, $email, $phone, $login, $pw) {
global $db;
// hash password concat with login to store
$query = 'INSERT INTO staff (first_name, last_name, email, phone, login, pw)
VALUES (:first, :last, :email, :phone, :login, :pw)';
try {
$statement = $db->prepare($query);
$statement->bindValue(':first', $first_name);
$statement->bindValue(':last', $last_name);
$statement->bindValue(':email', $email);
$statement->bindValue(':phone', $phone);
$statement->bindValue(':login', $login);
$statement->bindValue(':pw', $pw);
$statement->execute();
$statement->closeCursor();
} catch (PDOException $e) {
$error = $e->getMessage();
//display $error_message in ./error.php page
$error_message = "<p>Error adding new staff member: $error </p>";
include 'error.php';
exit();
}
}
function add_voter($first_name, $last_name, $registered, $email, $phone) {
global $db;
$query = 'INSERT INTO mail
(registered, first_name, last_name, email, phone)
VALUES
(:registered, :first, :last, :email, :phone)';
try {
$statement = $db->prepare($query);
$statement->bindValue(':first', $first_name);
$statement->bindValue(':last', $last_name);
$statement->bindValue(':email', $email);
$statement->bindValue(':phone', $phone);
$statement->bindValue(':registered', $registered);
$statement->execute();
$statement->closeCursor();
} catch (PDOException $e) {
$error = $e->getMessage();
//display $error_message in ./error.php page
$error_message = "<p>Error adding new voter: $error </p>";
include 'error.php';
exit();
}
}
// function add_donation(parameters) {};
// how to check for existing voter in mail table, for voter_id parameter
function add_donator($first_name, $last_name, $street, $city, $state, $zip,
$registered) {
global $db;
$query = 'INSERT INTO mail
(registered, first_name, last_name, loc_street, loc_city,
loc_state, loc_zip)
VALUES
(:registered, :first, :last, :street, :city, :state, :zip)';
try {
$statement = $db->prepare($query);
$statement->bindValue(':first', $first_name);
$statement->bindValue(':last', $last_name);
$statement->bindValue(':street', $street);
$statement->bindValue(':city', $city);
$statement->bindValue(':state', $state);
$statement->bindValue(':zip', $zip);
$statement->bindValue(':registered', $registered);
$statement->execute();
$voter_id = $db->lastInsertId();
$statement->closeCursor();
return $voter_id;
} catch (PDOException $e) {
$error = $e->getMessage();
//display $error_message in ./error.php page
$error_message = "<p>Error adding new donator: $error </p>";
include 'error.php';
exit();
}
}
function add_donation($voter_id, $donation_amount, $payment_type) {
global $db;
$query = 'INSERT INTO donations
(voter_id, donation_date, donation_amount, payment_type)
VALUES
(:voter_id, CURRENT_DATE(), :donation_amount, :payment_type)';
try {
$statement = $db->prepare($query);
$statement->bindValue(':donation_amount', $donation_amount);
$statement->bindValue(':payment_type', $payment_type);
$statement->bindValue(':voter_id', $voter_id);
$statement->execute();
$donation_id = $db->lastInsertId();
$statement->closeCursor();
return $donation_id;
} catch (PDOException $e) {
$error = $e->getMessage();
//display $error_message in ./error.php page
$error_message = "<p>Error adding new donation: $error </p>";
include 'error.php';
exit();
}
}
// EDIT VOTERS / STAFF
function edit_staff($first_name, $last_name, $phone, $email,
$login, $pw, $staff_id) {
global $db;
$query = 'UPDATE staff SET
first_name = :first, last_name = :last, email = :email,
phone = :phone, login = :login, pw = :pw
WHERE staff_id = :staff_id';
try {
$statement = $db->prepare($query);
$statement->bindValue(':first', $first_name);
$statement->bindValue(':last', $last_name);
$statement->bindValue(':email', $email);
$statement->bindValue(':phone', $phone);
$statement->bindValue(':login', $login);
$statement->bindValue(':pw', $pw);
$statement->bindValue(':staff_id', $staff_id);
$statement->execute();
$statement->closeCursor();
} catch (PDOException $e) {
$error = $e->getMessage();
//display $error_message in ./error.php page
$error_message = "<p>Error editing staff information: $error </p>";
include 'error.php';
exit();
}
}
function edit_voter($first_name, $last_name, $phone, $email, $birthday,
$street, $state, $voter_id, $zip, $city, $registered) {
global $db;
$query = 'UPDATE mail SET
registered = :registered, first_name = :first, last_name = :last,
email = :email, phone = :phone, birthday = :birthday,
loc_street = :street, loc_state = :state, loc_city = :city,
loc_zip = :zip WHERE voter_id = :voter_id';
try {
$statement = $db->prepare($query);
$statement->bindValue(':first', $first_name);
$statement->bindValue(':last', $last_name);
$statement->bindValue(':email', $email);
$statement->bindValue(':phone', $phone);
$statement->bindValue(':birthday', $birthday);
$statement->bindValue(':street', $street);
$statement->bindValue(':state', $state);
$statement->bindValue(':city', $city);
$statement->bindValue(':zip', $zip);
$statement->bindValue(':voter_id', $voter_id);
$statement->bindValue(':registered', $registered);
$statement->execute();
$statement->closeCursor();
} catch (PDOException $e) {
$error = $e->getMessage();
//display $error_message in ./error.php page
$error_message = "<p>Error editing voter information: $error </p>";
include 'error.php';
exit();
}
}
?>