-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_manage.php
276 lines (267 loc) · 9.66 KB
/
user_manage.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
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
274
275
276
<?php
//Connect database
include "database/connect.php";
//Read session
include 'session.php';
$uid=$_SESSION['UserID'];
if($uid=='' || $uid==null){
$message="Please login to continue";
echo "<script type='text/javascript'>alert('$message');</script>";
header("Refresh: 0, login_register.php");
}
//Read button script
include "top_button.html";
?>
<!DOCTYPE html>
<html>
<head>
<title>TARUC Events - Add/Edit/Delete User </title>
<style type="text/css">
a:hover{
font-size: 24px;
}
a{
color: blue;
}
form{
margin-left: 60px;
margin-top: 15px;
margin-right: 60px;
}
table{
max-width:750px;
margin-top:50px;
margin-bottom:50px;
margin-left:auto;
margin-right:auto;
background-color: white;
}
th{
font-size: 28px;
text-align: center;
padding-top: 20px ;
padding-bottom: 20px ;
width: 50%;
}
td, input[type=text], input[type=email],input[type=password], select{
font-family: Times New Roman;
font-size: 22px;
text-align: center;
padding-top: 2px ;
padding-bottom: 2px ;
}
input[type=submit], input[type=reset]{
padding: 10px;
color: black;
border: none;
background-color: #66CDAA;
font-weight: 900;
font-family: Times New Roman;
font-size: 20px;
text-align: center;
width: 120px;
}
input[type=submit]:hover, input[type=reset]:hover{
background-color: #20B2AA;
}
</style>
</head>
<body background="image\bg.png">
<button onclick="topFunction()" id="myBtn" title="Go to top"></button>
<hr width="auto" size="10" style="background: #808000">
<div id="add">
<form action="user_manage.php#add" method="POST">
<table align="center" cellspacing="20px">
<tr><th style="text-decoration: underline;"> >>> Add New User <<< </th></tr>
<tr><td>User ID: <input type="text" name="a_userid" size="30" required></td></tr>
<tr><td>Name: <input type="text" name="a_username" size="30" required></td></tr>
<tr><td>Password: <input type="password" name="a_userpass" size="30" required></td></tr>
<tr><td>Email: <input type="email" name="a_useremail" size="30" required></td></tr>
<tr><td>User Type:
<select name="a_usertype" style="width: 120px;">
<option value="">Select</option>
<option value="Student">Student</option>
<option value="Client">Client</option>
</select>
</td></tr>
<tr><td><input type="submit" name="adduser" value="Add">
<input type="reset" name="cancel" value="Cancel"></td></tr>
</table>
</form>
</div>
<hr width="auto" size="10" style="background: #808000">
<div id="edit">
<form action="user_manage.php#edit" method="POST">
<table align="center" cellspacing="20px">
<tr><th style="text-decoration: underline;"> >>> Edit User <<< </th></tr>
<tr><td>User ID:
<select name="edit_user_id" style="width: 140px;">
<option value="">Select</option>
<?php
$conn = mysqli_connect($servername, $username, $password, $dbname);
$read_user = "SELECT * FROM user_details";
$result_read_user = mysqli_query($conn, $read_user);
if(mysqli_num_rows($result_read_user)>0){
while($row = mysqli_fetch_array($result_read_user, MYSQLI_ASSOC)){
echo "<option value='".$row['UserID']."'>".$row['UserID']."</option>";
}
}
?>
</select>
<input type="submit" name="refreshuser" value="Refresh"></td></tr>
<tr><td><img src="image/divide.jpg" height="40%" width="100%" style="opacity: 0.6"></td></tr>
<tr><td>New User Name: <input type="text" name="e_username" size="30"></td></tr>
<tr><td>New User Email: <input type="text" name="e_useremail" size="30"></td></tr>
<tr><td><input type="submit" name="edituser" value="Save">
<input type="reset" name="cancel" value="Cancel"></td></tr>
</table>
</form>
</div>
<hr width="auto" size="10" style="background: #808000">
<div id="delete">
<form action="user_manage.php" method="POST">
<table align="center" cellspacing="20px">
<tr><th style="text-decoration: underline;"> >>> Delete User <<< </th></tr>
<tr><td>User ID:
<select name="delete_user_id" style="width: 140px;">
<option value="">Select</option>
<?php
$conn = mysqli_connect($servername, $username, $password, $dbname);
$read_user = "SELECT * FROM user_details";
$result_read_user = mysqli_query($conn, $read_user);
if(mysqli_num_rows($result_read_user)>0){
while($row = mysqli_fetch_array($result_read_user, MYSQLI_ASSOC)){
echo "<option value='".$row['UserID']."'>".$row['UserID']."</option>";
}
}
?>
</select>
<input type="submit" name="refreshuser" value="Refresh"></td></tr>
<tr><td><input type="submit" name="deleteuser" value="Delete">
<input type="reset" name="cancel" value="Cancel"></td></tr>
</form>
</div>
<!--Each button's action-->
<?php
$conn = mysqli_connect($servername, $username, $password, $dbname);
//Add user
if (isset($_POST['adduser'])) {
$uid = $_POST['a_userid'];
$upass = hash('sha512',$_POST['a_userpass']);
$uname = $_POST['a_username'];
$utype = $_POST['a_usertype'];
$uemail = $_POST['a_useremail'];
$insert_user = "INSERT INTO user_details (UserID, UserFullName, UserPassword, UserType, UserEmail) VALUES ('$uid', '$uname', '$upass', '$utype', '$uemail')";
//Ensure no empty field
if($utype==''){
$message="User type not selected. Please try again.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else{
$result_insert_user = mysqli_query($conn, $insert_user);
if($result_insert_user){
$message="Add user success.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else{
$message="Fail to add new user. Please try again.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
}
//Edit user
if (isset($_POST['edituser'])) {
$selectid=$_POST['edit_user_id'];
$uname=$_POST['e_username'];
$uemail=$_POST['e_useremail'];
if($selectid==''){
$message="User ID not selected. Please try again.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else{
//Update both name and email
if(($uname!='') && ($uemail!='')){
$update_user = "UPDATE user_details SET UserFullName='$uname', UserEmail='$uemail' WHERE UserID='$selectid'";
$result_update_user = mysqli_query($conn, $update_user);
if($result_update_user){
$message="Update user(ID: ".$selectid.") name and email success.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else{
$message="Fail to update user name and email. Please try again.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
//Update name only
elseif(($uname!='') && ($uemail=='')){
$update_name = "UPDATE user_details SET UserFullName='$uname' WHERE UserID='$selectid'";
$result_update_name = mysqli_query($conn, $update_name);
if($result_update_name){
$message="Update user(ID: ".$selectid.") name success.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else{
$message="Fail to update user name. Please try again.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
//Update email only
elseif (($uname=='') && ($uemail!='')) {
$update_email = "UPDATE user_details SET UserEmail='$uemail' WHERE UserID='$selectid'";
$result_update_email = mysqli_query($conn, $update_email);
if($result_update_email){
$message="Update user(ID: ".$selectid.") email success.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else{
$message="Fail to update email. Please try again.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
}
}
//Delete user
if (isset($_POST['deleteuser'])) {
$selectid=$_POST['delete_user_id'];
if($selectid==''){
$message="User ID not selected. Please try again.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else{
//Check 'event_details' table. If user organized event, cannot delete
$check_event="SELECT UserID FROM event_details WHERE UserID='$selectid'";
$result_check_event = mysqli_query($conn, $check_event);
if(mysqli_num_rows($result_check_event)>0){
$message="Cannot delete user. The user is the organizer of certain event.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
//If user made booking for one or more event, cannot delete user
else{
$check_booking="SELECT UserID FROM booking_details WHERE UserID='$selectid'";
$result_check_booking = mysqli_query($conn, $check_booking);
if(mysqli_num_rows($result_check_booking)>0){
$message="Selected user has made booking for some event. Cannot delete user.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else{
$delete_user_booking = "DELETE FROM booking_details WHERE UserID='$selectid'";
$result_delete_user_booking = mysqli_query($conn, $delete_user_booking);
if($result_delete_user_booking){
$delete_user = "DELETE FROM user_details WHERE UserID='$selectid'";
$result_delete_user = mysqli_query($conn, $delete_user);
if($result_delete_user){
$message="Delete user success.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else{
$message="Fail to delete user. Please try again.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
}
}
}
}
?>
</body>
</html>