-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodify-user.php
165 lines (128 loc) · 4.15 KB
/
modify-user.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
<?php session_start(); ?>
<?php require_once('includes/connection.php'); ?>
<?php require_once('includes/functions.php'); ?>
<?php
// checking if a user is logged in
if (!isset($_SESSION['user_id'])) {
header('Location: index.php');
}
$errors = array();
$user_id = '';
$first_name = '';
$last_name = '';
$email = '';
if (isset($_GET['user_id'])) {
// getting the user information
$user_id = mysqli_real_escape_string($connection, $_GET['user_id']);
$query = "SELECT * FROM ums_tb WHERE id = {$user_id} LIMIT 1";
$result_set = mysqli_query($connection, $query);
if ($result_set) {
if (mysqli_num_rows($result_set) == 1) {
// user found
$result = mysqli_fetch_assoc($result_set);
$first_name = $result['first_name'];
$last_name = $result['last_name'];
$email = $result['email'];
} else {
// user not found
header('Location: users.php?err=user_not_found');
}
} else {
// query unsuccessful
header('Location: users.php?err=query_failed');
}
}
if (isset($_POST['submit'])) {
$user_id = $_POST['user_id'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
// checking required fields
$req_fields = array('user_id', 'first_name', 'last_name', 'email');
$errors = array_merge($errors, check_req_fields($req_fields));
// checking max length
$max_len_fields = array('first_name' => 50, 'last_name' =>100, 'email' => 100);
$errors = array_merge($errors, check_max_len($max_len_fields));
// checking email address
if (!is_email($_POST['email'])) {
$errors[] = 'Email address is invalid.';
}
// checking if email address already exists
$email = mysqli_real_escape_string($connection, $_POST['email']);
$query = "SELECT * FROM ums_tb WHERE email = '{$email}' AND id != {$user_id} LIMIT 1";
$result_set = mysqli_query($connection, $query);
if ($result_set) {
if (mysqli_num_rows($result_set) == 1) {
$errors[] = 'Email address already exists';
}
}
if (empty($errors)) {
// no errors found... adding new record
$first_name = mysqli_real_escape_string($connection, $_POST['first_name']);
$last_name = mysqli_real_escape_string($connection, $_POST['last_name']);
// email address is already sanitized
$query = "UPDATE ums_tb SET ";
$query .= "first_name = '{$first_name}', ";
$query .= "last_name = '{$last_name}', ";
$query .= "email = '{$email}' ";
$query .= "WHERE id = {$user_id} LIMIT 1";
$result = mysqli_query($connection, $query);
if ($result) {
// query successful... redirecting to users page
header('Location: users.php?user_modified=true');
} else {
$errors[] = 'Failed to modify the record.';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>View / Modify User</title>
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<header>
<div class="appname">User Management System</div>
<div class="loggedin">Welcome <?php echo $_SESSION['first_name']; ?>! <a href="logout.php">Log Out</a></div>
</header>
<main>
<h1><img src="img\Login.png" alt="" height="80px" width="80px"> Modify a User<span> <a href="users.php">< Back to User List</a></span></h1>
<?php
if (!empty($errors)) {
display_errors($errors);
}
?>
<form action="modify-user.php" method="post" class="userform">
<input type="hidden" name="user_id" value="<?php echo $user_id; ?>">
<p>
<label for="">First Name:</label>
<input type="text" name="first_name" <?php echo 'value="' . $first_name . '"'; ?>>
</p>
<p>
<label for="">Last Name:</label>
<input type="text" name="last_name" <?php echo 'value="' . $last_name . '"'; ?>>
</p>
<p>
<label for="">Email Address:</label>
<input type="text" name="email" <?php echo 'value="' . $email . '"'; ?>>
</p>
<p>
<label for="">Password:</label>
<span>******</span> | <a href="change-password.php?user_id=<?php echo $user_id;?>">Change Password</a>
</p>
<p>
<label for=""> </label>
<button type="submit" name="submit">Save</button>
</p>
</form>
</main>
<br>
<br>
<br>
<br>
<?php require_once('includes/footer.php'); ?>
</body>
</html>