Changing a password PHP MySQL

Changing a password PHP MySQL

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Critical Security Flaws in Your Password Reset Logic

The code snippet you provided attempts to handle a common requirement: allowing users to update their credentials. However, if you are seeing behavior where the submit button redirects to a "read-only" page or displays confusing error messages, it is likely because the application is failing silently due to fundamental security vulnerabilities and logic errors. The current implementation exposes your database and user accounts to severe risks, including SQL injection and plaintext password storage.

Plaintext Password Storage

The most immediate technical debt in this example is the storage of passwords in user_info as plain text strings. The line $result = mysql_query("SELECT password FROM...") retrieves a raw string, which is then compared directly against the input.

Storing passwords in plaintext is a critical failure. If an attacker gains read access to your database—a relatively common occurrence in breaches—they can extract every user's password instantly. There is no cryptographic barrier between the user and the stolen credential. Modern standards require hashing algorithms like bcrypt or Argon2. You must never compare hashes directly against raw input; instead, you generate a hash from the new password using password_hash() before storing it, and use password_verify() to check the old password during authentication.

SQL Injection Vulnerability

The query construction method used here is dangerously naive:

$sql = mysql_query("UPDATE user_info SET password='$newpassword' WHERE user_id='$username'");

By embedding the $username and $newpassword variables directly into the SQL string, you are inviting SQL injection attacks. An attacker could submit a username like admin' OR '1'='1 to update the password for every single row in the database with whatever value they provide in the "new password" field.

To fix this, you must use prepared statements. The legacy mysql_* functions are deprecated and removed in newer PHP versions; you should migrate immediately to PDO or mysqli. Here is how a secure implementation using PDO looks:

$username = $_POST['username'] ?? '';
$oldPassword = $_POST['password'] ?? '';
$newPassword = $_POST['newpassword'] ?? '';
$confirmPassword = $_POST['confirmnewpassword'] ?? '';

// Validate that passwords match before any database interaction
if ($newPassword !== $confirmPassword) {
    die('Passwords do not match.');
}

// Verify the existing password first (must be hashed in DB, verified here)
$stmt = $pdo->prepare("SELECT password FROM user_info WHERE user_id = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();

if (!$user || !password_verify($oldPassword, $user['password'])) {
    die('Invalid credentials.');
}

// Generate a secure hash for the new password
$hashedNewPassword = password_hash($newPassword, PASSWORD_DEFAULT);

// Update using prepared statement to prevent injection
$updateStmt = $pdo->prepare("UPDATE user_info SET password = ? WHERE user_id = ?");
$updateStmt->execute([$hashedNewPassword, $username]);

if ($updateStmt) {
    // Regenerate session ID to prevent session fixation
    session_regenerate_id(true);
    echo "Password changed successfully.";
} else {
    echo "Failed to update password.";
}

The Redirect Behavior

You mentioned that submitting the form takes you to a "read-only" page. This usually happens when an error occurs but the script doesn't halt execution, or when the database connection fails inside dbconfig.php. If mysql_query returns false due to a syntax error (caused by injection) or a connection timeout, the legacy mysql_* functions do not throw exceptions by default; they simply return false. The subsequent lines in your script might then execute against an empty state or fail silently.

Additionally, there is a logic bug in your original PHP:

if($newpassword=$confirmnewpassword)
    $sql=mysql_query(...);
else
    echo "Passwords do not match";

The else block executes when the condition is false (either because they don't match, or both are empty strings). However, your success message appears inside an if($sql) block. If the update succeeds but the previous logic was flawed, you might see a success message even if the validation failed in a different context, or the script might continue to redirect based on session logic that isn't shown here.

Migration Path

If you are maintaining legacy code using ISO-8859-1 encoding and mysql_* functions, you are running on an obsolete stack. The combination of plaintext passwords, SQL injection risks, and deprecated APIs makes this application non-compliant with any modern security standard.

The most robust path forward is to refactor the authentication layer. If you are building a larger application or managing a team, consider adopting a framework like Laravel. It provides built-in support for secure hashing, prepared statements via Eloquent ORM, and session management that mitigates many of these specific vulnerabilities out of the box. For more information on securing your PHP applications, see LaravelCompany.

Our network