更改用户密码表单

时间:2017-11-19 22:29:52

标签: php mysql sql

我想创建一个可以更改当前用户ID密码的表单。

我必须提到我有sessios user_id

if (!isset($_SESSION["user_id"]) || $_SESSION["user_id"] == "") {

表名是:system_users

和system_users包含:

u_userid | u_username | u_password | u_roldecode
   1          admin       admin      SUPERADMIN
   2          user1       user1pw     ADMIN

我试过很多表格来改变密码但没有工作:(

你能帮帮我吗?我真的很赞赏你的努力!

谢谢!

1 个答案:

答案 0 :(得分:1)

PHP部分:

<?php
    session_start();
    // put your connect server, select database code below.

    // the below code checks if the user_id is set.
    // If it is set, store it in a variable named $user_id
    // and grab the old password for display.
    if(isset($_SESSION["user_id"])) {
        $user_id = $_SESSION["user_id"];
        $SQL = "SELECT * FROM system_users WHERE u_userid = '$user_id'";
        // replace $connect with the variable you used mysqli_connect();
        $result = mysqli_query($connect, $SQL);
        $field = mysqli_fetch_assoc($result);
        $oldpass = $field['u_password'];
    }
    if(isset($_POST['change'])) {
        $newpass = $_POST['newpass'];
        $SQL = "UPDATE system_users SET u_password = '$newpass' WHERE u_userid = '$user_id'";
        $result = mysqli_query($connect, $SQL);
        // redirect to some page after changing password
        header("Location: thephpfile.php");
    }
?>

HTML部分:

<form name="changepass" action="thephpfile.php" method="post">
    <input type="password" name="newpass" placeholder="New Password" value="<?php echo $oldpass; ?>"/>
    <input type="submit" name="change" value="Change Password"/>
</form>

将.phpfile.php替换为php文件的名称。这两个代码将写在同一个php文件中。