-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_email.php
More file actions
162 lines (145 loc) · 5.55 KB
/
Copy pathedit_email.php
File metadata and controls
162 lines (145 loc) · 5.55 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
<?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 email from the database
$sql = "SELECT user_email 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_email = $user_data['user_email'];
} else {
echo "User not found.";
exit();
}
}
// Handle form submission to update the email
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Check if the new email and password are set
if (isset($_POST['email']) && isset($_POST['password'])) {
$new_email = $_POST['email'];
$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 email in the database
$update_sql = "UPDATE royale_user_tbl SET user_email = ? WHERE user_id = ?";
$update_stmt = $conn->prepare($update_sql);
$update_stmt->bind_param("si", $new_email, $user_id);
if ($update_stmt->execute()) {
echo "<script>
window.onload = function() {
Swal.fire({
icon: 'success',
title: 'Email Updated',
text: 'Your email 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 email.',
});
};
</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 Email</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>
<div class="edit-main-container hidden">
<h1 class="hidden">Edit Email</h1>
<form id="edit-email-form" method="POST">
<label for="email">Old Email:</label>
<input type="email" value="<?php echo htmlspecialchars($user_email); ?>" disabled>
<label for="email">New Email:</label>
<input type="email" id="email" name="email" value="<?php echo htmlspecialchars($user_email); ?>" 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 Email</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-email-form');
var passwordInput = document.createElement('input');
passwordInput.type = 'hidden';
passwordInput.name = 'password';
passwordInput.value = result.value;
form.appendChild(passwordInput);
form.submit();
}
});
});
</script>