更新密码选项未在DB中更新

时间:2015-07-12 01:06:21

标签: php

尝试设置更新密码功能,一旦您登录并转到个人资料页面并且我一直在玩它但没有任何效果,我没有任何错误,但它没有' t更新数据库。

<?php
if(isset($_POST['submit'])){
$newpassword = $_POST['password'];
if(empty($newpassword)) {
    echo '<p>Field Empty!</p>';
} else {
$mysqli->query("UPDATE password FROM users WHERE password='$newpassword'");
echo "Updated Successfully"
}
}

 ?>

1 个答案:

答案 0 :(得分:2)

据我所知,你的查询是错误的。

您需要添加要更新的行的ID。您应该在会话中存储哪些内容。

<?php
if(isset($_POST['submit'])){
$newpassword = $_POST['password'];
if(empty($newpassword)) {
    echo '<p>Field Empty!</p>';
} else {
$mysqli->query("UPDATE Users SET password='$newpassword' WHERE ID=?");
echo "Updated Successfully"; 
}
}

 ?>

我会做的更改,你有一些缺少分号,你需要检查原始帖子上的密码。阅读更清洁。

  <?php
    if(isset($_POST['submit']) && isset($_POST['password'])){
        $newpassword = $_POST['password'];

        $mysqli->query("UPDATE Users SET password='$newpassword' WHERE ID=?");
        echo "Updated Successfully";

    }
    else {
        echo '<p>The password field is not filled in.</p>';
    }
   ?>

我讨厌不安全的网页。这是一种更安全的方法。它可能有错误,我没有测试。

<?php


        // configuration
        $dbhost     = "localhost";
        $dbname     = "test";
        $dbuser     = "root";
        $dbpass     = "admin";

        // database connection
        $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
    if(isset($_POST['submit']) && isset($_POST['password'])){



        // new data
        $UserID = $_SESSION['UserID'];
        $Password = $_POST['password'];

        // query
        $sql = "UPDATE Users SET password=? WHERE ID=?";

        $q = $conn->prepare($sql);
        $q->bindParam(1, $UserID);
        $q->bindParam(2, $Password);
        $q->execute(array($title,$author,$id));
    }
    else {
        echo '<p>The password field is not filled in.</p>';
    }
?>