-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_name.php
More file actions
171 lines (145 loc) · 5.54 KB
/
Copy pathedit_name.php
File metadata and controls
171 lines (145 loc) · 5.54 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
<?php
require 'dbconnect.php'; // Ensure this file correctly initializes $conn
session_start(); // Start the session
// Check if the user ID is passed in the URL
if (isset($_GET['user_id'])) {
$user_id = $_GET['user_id'];
// Fetch the user's name from the database
$sql = "SELECT user_name FROM royale_user_tbl WHERE user_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// Fetch user data
$user_data = $result->fetch_assoc();
$user_name = $user_data['user_name'];
} else {
echo "User not found.";
exit();
}
}
// Handle form submission to update the name
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Check if the new name and password are set
if (isset($_POST['name']) && isset($_POST['password'])) {
$new_name = $_POST['name'];
$password = $_POST['password'];
// Fetch the user's hashed password from the database
$sql = "SELECT user_password FROM royale_user_tbl WHERE user_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$user_data = $result->fetch_assoc();
$hashed_password = $user_data['user_password'];
// Verify the entered password
if (password_verify($password, $hashed_password)) {
// Update the user's name in the database
$update_sql = "UPDATE royale_user_tbl SET user_name = ? WHERE user_id = ?";
$update_stmt = $conn->prepare($update_sql);
$update_stmt->bind_param("si", $new_name, $user_id);
if ($update_stmt->execute()) {
echo "<script>
window.onload = function() {
Swal.fire({
icon: 'success',
title: 'Name Updated',
text: 'Your name has been successfully updated.',
timer: 1500,
showConfirmButton: false
}).then(() => {
window.location.href = 'my_profile.php';
});
};
</script>";
} else {
echo "<script>
window.onload = function() {
Swal.fire({
icon: 'error',
title: 'Error',
text: 'Failed to update name.',
});
};
</script>";
}
} else {
echo "<script>
window.onload = function() {
Swal.fire({
icon: 'error',
title: 'Invalid Password',
text: 'The password you entered is incorrect.',
});
};
</script>";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Name</title>
<!-- important file -->
<?php include 'important.php'; ?>
<link rel="stylesheet" href="css_main/main.css?v=<?php echo time(); ?>">
<link rel="stylesheet" href="css_main/my_profile.css?v=<?php echo time(); ?>">
<link rel="shortcut icon" href="system_images/whitelogo.png" type="image/png">
</head>
<body>
<?php
include 'navigation.php';
?>
<main style="">
<div class="edit-main-container hidden">
<h1 class="hidden">Edit Name</h1>
<form id="edit-name-form" method="POST">
<label for="name">Old Name:</label>
<input type="text" value="<?php echo htmlspecialchars($user_name); ?>" disabled>
<label for="name">New Name:</label>
<input type="text" id="name" name="name" value="<?php echo htmlspecialchars($user_name); ?>" required>
</form>
<div class="buttons-container hidden">
<a href="my_profile.php"><i class="fa-solid fa-arrow-left"></i> Return</a>
<button type="button" id="update-button"><i class="fa-solid fa-floppy-disk"></i> Update Name</button>
</div>
</div>
</main>
</body>
</html>
<script>
document.getElementById('update-button').addEventListener('click', function () {
Swal.fire({
title: 'Confirm Password',
input: 'password',
inputLabel: 'Please enter your current password',
inputPlaceholder: 'Your password',
inputAttributes: {
autocapitalize: 'off'
},
showCancelButton: true,
confirmButtonText: 'Submit',
cancelButtonText: 'Cancel',
inputValidator: (value) => {
if (!value) {
return 'You need to enter your password!';
}
}
}).then((result) => {
if (result.isConfirmed) {
// Add password input to form and submit
var form = document.getElementById('edit-name-form');
var passwordInput = document.createElement('input');
passwordInput.type = 'hidden';
passwordInput.name = 'password';
passwordInput.value = result.value;
form.appendChild(passwordInput);
form.submit();
}
});
});
</script>