-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofile.php
141 lines (114 loc) · 4.8 KB
/
profile.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
<?php
require_once('./conn.php');
// Retrieve user information from the database
$userInfo = array();
$sql = "SELECT U_ID, username, email, phone FROM users WHERE username = ?";
$params = array($_SESSION['username']);
$result = sqlsrv_query($conn, $sql, $params);
if ($result && sqlsrv_has_rows($result)) {
$userInfo = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC);
}
// Check if update username form was submitted
if (isset($_POST['updateUsername'])) {
// Get new username from form
$newUsername = $_POST['newUsername'];
// Update users table with new username
$sql = "UPDATE users SET username = ? WHERE username = ?";
$params = array($newUsername, $_SESSION['username']);
sqlsrv_query($conn, $sql, $params);
// Update session with new username
$_SESSION['username'] = $newUsername;
}
// Check if update password form was submitted
if (isset($_POST['updatePassword'])) {
// Get new password from form
$newPassword = $_POST['newPassword'];
// Hash the new password
$hashedPassword = password_hash($newPassword, PASSWORD_DEFAULT);
// Update users table with hashed password
$sql = "UPDATE users SET password = ? WHERE username = ?";
$params = array($hashedPassword, $_SESSION['username']);
sqlsrv_query($conn, $sql, $params);
}
// Check if update email form was submitted
if (isset($_POST['updateEmail'])) {
// Get new email from form
$newEmail = $_POST['newEmail'];
// Update users table with new email
$sql = "UPDATE users SET email = ? WHERE username = ?";
$params = array($newEmail, $_SESSION['username']);
sqlsrv_query($conn, $sql, $params);
$userInfo['email'] = $newEmail; // Update displayed email
}
// Check if update phone form was submitted
if (isset($_POST['updatePhone'])) {
// Get new phone from form
$newPhone = $_POST['newPhone'];
// Update users table with new phone
$sql = "UPDATE users SET phone = ? WHERE username = ?";
$params = array($newPhone, $_SESSION['username']);
sqlsrv_query($conn, $sql, $params);
$userInfo['phone'] = $newPhone; // Update displayed phone number
}
// Check if delete button was clicked
if (isset($_POST['delete'])) {
// Delete user record from users table
$sql = "DELETE FROM users WHERE username = ?";
$params = array($_SESSION['username']);
sqlsrv_query($conn, $sql, $params);
// Log out user and redirect to login page
session_destroy();
header("Location: ./login.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile</title>
<link rel="stylesheet" href="./style/profile.css">
</head>
<body>
<div class="dashboard">
<?php require_once('./dashboard.php'); ?>
<div class="container">
<h1>Your Profile</h1>
<!-- Display user information -->
<p><strong>User ID:</strong> <?php echo $userInfo['U_ID']; ?></p>
<p><strong>User name:</strong> <?php echo $userInfo['username']; ?></p>
<p><strong>Email:</strong> <?php echo $userInfo['email']; ?></p>
<p><strong>Phone:</strong> <?php echo $userInfo['phone']; ?></p>
<!-- Update username form -->
<form action="" method="post">
<label for="newUsername">New Username:</label>
<input type="text" id="newUsername" name="newUsername" required placeholder="Name">
<button class="submit" type="submit" name="updateUsername">Change</button>
</form>
<!-- Update email form -->
<form action="" method="post">
<label for="newEmail">New Email:</label>
<input type="email" id="newEmail" name="newEmail" required placeholder="Email">
<button class="submit" type="submit" name="updateEmail">Change</button>
</form>
<!-- Update phone form -->
<form action="" method="post">
<label for="newPhone">New Phone:</label>
<input type="text" id="newPhone" name="newPhone" required placeholder="Phone">
<button class="submit" type="submit" name="updatePhone">Change</button>
</form>
<!-- Update password form -->
<form action="" method="post">
<label for="newPassword">New Password:</label>
<input type="password" id="newPassword" name="newPassword" required placeholder="password">
<button class="submit" type="submit" name="updatePassword">Change</button>
</form>
<!-- Delete account form -->
<form action="" method="post">
<button class="delete submit" type="submit" name="delete">Delete Account</button>
</form>
</div>
</div>
</body>
</html>