【问题标题】:Allow user to change password允许用户更改密码
【发布时间】:2015-01-11 16:46:06
【问题描述】:

我正在尝试让我网站的登录用户能够更改他们的密码,然后该密码将在我的数据库中更新。当我单击提交按钮时,我在“where 子句”中得到“未知列”[用户名]。我尝试了多种方法,但似乎无法使其正常工作。我是 PHP 的初学者,所以没有广泛的技能,所以我不确定问题可能是什么。如果有人可以帮助我,我将不胜感激,谢谢。

<?php
session_start();


require_once ("db_connect.php");
require_once($_SERVER['DOCUMENT_ROOT'] . '/functions/functions.php');


$oldpw = ($_POST['oldpw']);
$newpw = ($_POST['newpw']);
$conpw = ($_POST['conpw']);
$currentpw = $_SESSION['password'];

if ($_POST['change'] == 'Change') {
    if ($oldpw && $newpw && $conpw) {
        if ($newpw == $conpw) {
            if ($db_server){
                mysqli_select_db($db_server, $db_database);
                $oldpw = salt($currentpw);
                // check whether username exists 
                $query = "SELECT password FROM users WHERE 'username'= '" . $_SESSION['username'] . "'";
                $result = mysqli_query($db_server, $query);
                if(!$result){
                    $message = "<p class='message'>Error: Coud not connect to the database.</p>" ;
                }else{
                    $newpw = salt($newpw);
                    $query = "UPDATE users SET password = '$newpw' WHERE username = " . $_SESSION['username'] . "";
                    mysqli_query($db_server, $query) or
                            die("Insert failed. " . mysqli_error($db_server));
                    $message = "<p class='message'>Your password has been changed!</p>";
                    // Process further here 
                    mysqli_free_result($result);
                }
            }else{
                    $message = " <p class='message'>Your current password is incorrect.</p>";
            }
        }else{
            $message = "<p class='message'>Your new passwords do not match.</p>";
        }
    }else{
        $message = "<p class='message'>Please fill in all fields.</p>";
    }
}
?>

这是我使用的html:

<form action='change-password.php' method='post' id="register-form">
     <?php echo $message; ?>
         <input class="password-field" type='password' name='oldpw' value='<?php echo $username; ?>' placeholder="Current Password"><br />  
         <input  class="password-field" type='password' name='newpw' placeholder="New Password"><br />
         <input class="password-field" type='password' name='conpw' placeholder="Confrim Password">
         <input class="button" type='submit' name='change' value='Change' />
 </form>

【问题讨论】:

  • 要找出真正的错误,您应该使用echo mysqli_error($db_server); 而不是您的Error: could not connect... 自定义消息。
  • 我猜这是因为用户名$_SESSION['username'] 没有用单引号括起来作为该查询中的SQL 字符串...
  • 字符串必须用引号引起来。另外你为什么要存储密码明文?
  • 对于用户名和$newpw 的后续UPDATE 语句也是如此,请参阅When to use single quotes, double quotes, backticks....
  • @CharlotteDunois 密码中有一个salt() 函数。我会假设这是在做一些盐+哈希操作......

标签: php database mysqli sql-update change-password


【解决方案1】:

您面临的最初问题是$_SESSION['username']$newpass 的SQL 字符串值在SQL 字符串中没有正确单引号。有关何时以及如何在 SQL 语句中引用的简要说明,请查看 When to use single quotes, double quotes, backticks in MySQL

在开发代码时始终打开错误报告。在脚本的顶部:

// Disable this when your code is live...
error_reporting(E_ALL);
ini_set('display_errors', 1);

所以使用您当前的代码,引用 strings 而不是 列名 将如下所示。

$oldpw = salt($currentpw);

// check whether username exists 
$query = "SELECT password FROM users WHERE username= '" . $_SESSION['username'] . "' AND password='$oldpw'";
//----------------------------------no quotes^^^^^^^--single-quotes^^^^^^^^^^^^^^^^
// Also adds a check that $oldpass is correct!

$result = mysqli_query($db_server, $query);
if(!$result){
    // This is ambiguous. It should probably show an error related to the query, not connection
    $message = "<p class='message'>Error: Coud not connect to the database.</p>" ;
}else{
    // This should only be done if a row was returned previously
    // Test with mysqli_num_rows()
    if (mysqli_num_rows($result) > 0) {
      $newpw = salt($newpw);

      // Adds single quotes to the username here too...
      $query = "UPDATE users SET password = '$newpw' WHERE username = '" . $_SESSION['username'] . "'";
      mysqli_query($db_server, $query) or
            die("Insert failed. " . mysqli_error($db_server));
      $message = "<p class='message'>Your password has been changed!</p>";
      // Process further here 
      mysqli_free_result($result);
    }
    else {
       // Username or password was incorrect - do something about that
    }
}

这可以进一步改进using prepared statements。您的散列密码应该可以防止 SQL 注入,but it is highly recommended to get into the habit of using prepared statements,因为在字符串直接从用户输入派生的其他情况下,它们是必要的,以提供足够的 SQL 注入保护。

看起来像:

// Prepare the select statement to check username and old password
$stmt = mysqli_prepare($db_server, "SELECT password FROM users WHERE username = ? AND passowrd = ?");
if ($stmt) {
  // Bind parameters and execute it
  $stmt->bind_param('ss', $_SESSION['username'], $oldpw);
  $stmt->execute();

  // num_rows works here too...
  if ($stmt->num_rows > 0) {
    // Ok to update...
    // Prepare another statement for UPDATE
    $stmt2 = mysqli_prepare($db_server, "UPDATE users SET password = ? WHERE username = ?");
    if ($stmt2) {
       // Bind and execute
       $stmt2->bind_param('ss', $newpass, $_SESSION['username']);
       $stmt->execute();
    }
    // Error updating
    else echo mysqli_error($db_server);
  }
}
// Error selecting
else echo mysqli_error($db_server);

【讨论】:

    猜你喜欢
    • 2012-11-17
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 2016-04-07
    相关资源
    最近更新 更多