How can PHP be used to update user passwords in a database?

To update user passwords in a database using PHP, you can create a form where users can input their current password and new password. Then, you can use PHP to check if the current password matches the one stored in the database for that user. If it does, you can update the password in the database with the new password.

<?php
// Assuming you have established a database connection

if(isset($_POST['submit'])) {
    $currentPassword = $_POST['current_password'];
    $newPassword = $_POST['new_password'];

    // Check if current password matches the one stored in the database for the user
    $query = "SELECT * FROM users WHERE id = '1' AND password = '$currentPassword'";
    $result = mysqli_query($connection, $query);

    if(mysqli_num_rows($result) == 1) {
        // Update the password in the database
        $updateQuery = "UPDATE users SET password = '$newPassword' WHERE id = '1'";
        mysqli_query($connection, $updateQuery);
        echo "Password updated successfully!";
    } else {
        echo "Current password is incorrect.";
    }
}
?>

<form method="post" action="">
    <input type="password" name="current_password" placeholder="Current Password">
    <input type="password" name="new_password" placeholder="New Password">
    <input type="submit" name="submit" value="Update Password">
</form>