-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAdmin_edituser.php
305 lines (274 loc) · 9.42 KB
/
Admin_edituser.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<html>
<head>
<title> Admin - Add/edit user</title>
<link rel="icon" href="images/admin.jfif" type="image/x-icon">
<link href="CSS/simple-sidebar.css" rel="stylesheet">
<style>
input[type=text], select, textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
}
label {
padding: 12px 12px 12px 0;
display: inline-block;
}
input[type=submit] {
background-color: #292b2c;
color: white;
padding: 10px;
border: none;
text-align: center;
text-decoration: none;
font-size: 16px;
cursor: pointer;
float: right;
}
input[type=submit]:hover {
background-color: black;
}
.container {
margin-top: 40px;
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}
.col-25 {
float: left;
width: 25%;
margin-top: 6px;
}
.col-75 {
float: left;
width: 75%;
margin-top: 6px;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Responsive layout - when the screen is less than 600px wide, make the two columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
.col-25, .col-75, input[type=submit] {
width: 100%;
margin-top: 0;
}
}
</style>
</head>
<?php
session_start();
if (!$_SESSION['loggedIn'] || $_SESSION['user']['Role'] != 2) {
echo "<script> location.href='home.php'; </script>";
}
include "menu.php";
?>
<body>
<div class="d-flex" id="wrapper">
<div class="bg-dark border-right" id="sidebar-wrapper">
<div class="list-group list-group-flush bg-dark">
<a href="Admin_products.php" class="list-group-item list-group-item-action bg-dark text-light show"><span class="text-nowrap"><i class="fa fa-plus-square"></i> Products</a></span>
<a href="Admin_users.php" class="list-group-item list-group-item-action bg-dark text-light"><span class="text-nowrap"><i class="fa fa-user"></i> Users</a></span>
<a href="Admin_orders.php" class="list-group-item list-group-item-action bg-dark text-light"><span class="text-nowrap"><i class="fa fa-cog"></i> Orders</a></span>
<a href="Admin_contactus.php" class="list-group-item list-group-item-action bg-dark text-light"><span class="text-nowrap"><i class="fa fa-cog"></i> contact us</a></span>
</div>
</div>
<!-- /#sidebar-wrapper -->
<!-- Page Content -->
<div id="page-content-wrapper">
</nav>
<div class="container-fluid">
<?php
$servername = "localhost";
$username = "root";
$password = "";
$DB = "mydb";
$conn = mysqli_connect($servername, $username, $password, $DB);
if (!$conn) {
error_log(mysqli_connect_error(),3,"../errors/db-errors.log");
die("Connection failed: " .mysqli_connect_error());
}
if (isset($_GET['Id']) || isset($_POST['Id'])) {
if (isset($_POST['fname'])) {
$id = $_POST['Id'];
$name = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$userpassword = $_POST['password'];
$hashedPass = sha1($userpassword);
$number = $_POST['number'];
$gender = $_POST['gender'];
$role = $_POST['role'];
$action = $_POST['action'];
$file_name = $_FILES['image']['name'];
if ($action == "old") {
$file_name = $id;
}
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
echo "<script>alert('Email being used is wrong please type another email');</script>";
echo "<script> location.href='Admin_edituser.php?action=add&error=1'; </script>";
}
if ($name == "" || $lname < 0 || $email == "" || $number == "" || $file_name == "") {
if ($action == "old") {
echo "<script> location.href='Admin_edituser.php?Id=".$id."&error=1'; </script>";
} else {
echo "<script> location.href='Admin_edituser.php?action=add&error=1'; </script>";
}
$_SESSION["err"] = 1;
return;
}
if ($action == "old") {
$res = $conn->query("UPDATE user SET firstName = '$name', lastName = '$lname', Email = '$email', phoneNumber = '$number', gender = '$gender', Role = '$role',password='$hashedPass' WHERE Id = $id");
} else {
$select = mysqli_query($conn, "SELECT `Email` FROM `user` WHERE `email` = '".$_POST['email']."'") or exit(mysqli_error($conn));
if(mysqli_num_rows($select)) {
echo "<script>alert('Email is already being used');</script>";
echo "<script> location.href='Admin_edituser.php?action=add&error=1'; </script>";
}
else
$res = $conn->query("INSERT INTO user (firstName, lastName,password, Email, phoneNumber, gender, Role) VALUES ('$name', '$lname','$hashedPass', '$email', '$number', '$gender', '$role')");
}
if ($id == "new") {
$id = $conn->insert_id;
}
move_uploaded_file($_FILES['image']['tmp_name'],"images/users/".$id);
echo "<script> location.href='Admin_users.php'; </script>";
} else {
$res = $conn->query("SELECT * FROM user WHERE Id=".$_GET['Id']."");
$row = $res->fetch_assoc();
$row['action'] = "old";
$row['img'] = "images/users/".$row['Id'];
}
} else if (isset($_GET['action'])){
if ($_GET['action'] == "add") {
$row = array();
$row['Id'] = "new";
$row['firstName'] = 'first name';
$row['lastName'] = "last name";
$row['Email'] = "";
$row['Password'] = "";
$row['phoneNumber'] = "";
$row['gender'] = "Male";
$row['Role'] = "1";
$row['img'] = 'https://placehold.it/300';
$row['action'] = "new";
} else {
echo "<script> location.href='Admin_users.php'; </script>";
}
} else {
echo "<script> location.href='Admin_users.php'; </script>";
}
?>
<div class="container">
<?php
if (isset($_GET["error"])) {
echo "<h5 style='color: red;'>Please enter first name, last name, email, phone number and upload an image before proceeding. </h5>";
}
?>
<form action="Admin_edituser.php" method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col-25">
<label>First name</label>
</div>
<input type="hidden" name="action" id="action" value='<?php echo $row['action'] ?>'>
<input type="hidden" name="Id" id="Id" value='<?php echo $row['Id'] ?>'>
<div class="col-75">
<input type="text" id="fname" name="fname" value='<?php echo $row['firstName'] ?>'>
</div>
</div>
<div class="row">
<div class="col-25">
<label>Last name</label>
</div>
<div class="col-75">
<input type="text" id="lname" name="lname" value='<?php echo $row['lastName'] ?>'>
</div>
</div>
<div class="row">
<div class="col-25">
<label>Email</label>
</div>
<div class="col-75">
<input type="text" id="email" name="email" value='<?php echo $row['Email'] ?>'>
</div>
</div>
<div class="row">
<div class="col-25">
<label>Password</label>
</div>
<div class="col-75">
<input type="password" id="psw" value='<?php echo $row['Password'] ?>' name="password" class="form-control" required>
</div>
</div>
<div class="row">
<div class="col-25">
<label>Phone number</label>
</div>
<div class="col-75">
<input type="text" id="number" name="number" value='<?php echo $row['phoneNumber'] ?>'>
</div>
</div>
<div class="row">
<div class="col-25">
<label>Gender</label>
</div>
<div class="col-75">
<select id="gender" name="gender">
<option <?php if ($row['gender'] == "Male") {echo "selected='selected'"; }?> value="Male">Male</option>
<option <?php if ($row['gender'] == "Female") {echo "selected='selected'"; }?> value="Female">Female</option>
</select>
</div>
</div>
<div class="row">
<div class="col-25">
<label>Role</label>
</div>
<div class="col-75">
<select id="role" name="role">
<option <?php if ($row['Role'] == 1) {echo "selected='selected'"; }?> value="1">User</option>
<option <?php if ($row['Role'] == 2) {echo "selected='selected'"; }?> value="2">Admin</option>
<option <?php if ($row['Role'] == 3) {echo "selected='selected'"; }?> value="3">HR</option>
<option <?php if ($row['Role'] == 4) {echo "selected='selected'"; }?> value="4">Auditor</option>
</select>
</div>
</div>
<div class="row">
<div class="col-25">
<label>Upload image</label>
</div>
<div class="col-75">
<input type="file" id="image" name="image" class="file" onchange="putImage()" accept="image/*">
<img style="max-width: 300px; max-height: 300px;" src=<?php echo $row['img'] ?> id="target" id="preview" class="img-thumbnail">
</div>
</div>
<div class="row">
<input type="submit" value="Submit">
</div>
</form>
</div>
<script>
function showImage(src, target) {
var fr = new FileReader();
fr.onload = function(){
target.src = fr.result;
}
fr.readAsDataURL(src.files[0]);
}
function putImage() {
var src = document.getElementById("image");
var target = document.getElementById("target");
showImage(src, target);
}
</script>
</div>
</div>
</div>
<?php
include 'footer.php'
?>
</body>
</html>