-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_change_password.php
More file actions
40 lines (31 loc) · 1.02 KB
/
Copy pathuser_change_password.php
File metadata and controls
40 lines (31 loc) · 1.02 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
<?php
include 'db_config.php';
session_start();
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$user_id = isset($_POST['user_id']) ? intval($_POST['user_id']) : 0;
$new_password = isset($_POST['password']) ? trim($_POST['password']) : "";
// Check if inputs are valid
if ($user_id <= 0 || empty($new_password)) {
echo "Invalid user ID or password.";
exit;
}
// Hash password
$hashedPassword = password_hash($new_password, PASSWORD_DEFAULT);
// Prepare SQL query
$stmt = $conn->prepare("UPDATE users SET password = ? WHERE id = ?");
if (!$stmt) {
echo "SQL Error: " . $conn->error;
exit;
}
$stmt->bind_param("si", $hashedPassword, $user_id);
if ($stmt->execute()) {
echo "Password updated successfully!";
} else {
echo "Database Error: " . $stmt->error;
}
$stmt->close();
}
$conn->close();