insert password into database in md5 format?

insert password into database in md5 format?

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

Stefan Bogdanescu

Founder & Senior Architect

Stop Hashing Passwords with MD5 in MySQL

The snippet you provided shows a classic pattern from an era of web development that is now considered insecure: storing user passwords as plain text or, even worse, attempting to "secure" them by hashing with MD5 directly during insertion. The code attempts to insert a raw password string into the ptb_users table using the deprecated mysql_query functions. This approach leaves your application vulnerable to trivial attacks where an attacker can simply dump the database and verify credentials against a list of common passwords instantly.

You cannot safely "insert a password in MD5 format" by just calling a function on the string before saving it. The fundamental problem is not how you store the hash, but how that hash was generated. MD5 (Message Digest 5) is cryptographically broken for password storage. It is a fast hashing algorithm designed to verify file integrity, not to protect secrets. Because it is so fast, an attacker with access to your database can guess millions of passwords per second using rainbow tables or brute-force scripts. If you store md5('password123'), anyone who knows the hash 5f4dcc3b5aa765d61d8327deb882cf99 immediately knows the password is 'password123'.

The Modern Standard: Argon2 and Bcrypt

To store passwords securely, you must use a memory-hard or computationally expensive hashing algorithm. The industry standard has shifted from MD5 to Argon2, which won the Password Hashing Competition in 2015, followed by bcrypt as a widely supported fallback. PHP provides built-in functions for both that handle the salting and cost factors automatically, removing the complexity from your logic.

Instead of manually hashing the password string in your application code before inserting it into MySQL, you should use password_hash(). This function creates a unique salt for every password and stores the algorithm identifier, cost parameter, and salt within the hash itself.

Here is how you should modify your insertion logic using modern PHP:

// Generate a secure hash with automatic salting
$hashedPassword = password_hash($password, PASSWORD_ARGON2ID);

$query = "INSERT INTO ptb_users (id, user_id, first_name, last_name, email, password)
          VALUES (?, ?, ?, ?, ?, ?)";

$stmt = $pdo->prepare($query);
$stmt->execute([
    'NULL', 
    'NULL', 
    $firstname, 
    $lastname, 
    $email, 
    $hashedPassword
]);

This approach ensures that every password is unique to the user, even if two users choose the same passphrase. The database no longer needs to know how to hash; it just needs to store the resulting string. When a user logs in later, you use password_verify(), which checks if the provided password matches the stored hash without revealing the original password.

Migrating Legacy Data

If your existing ptb_users table currently contains MD5 hashes or plain text passwords, you cannot simply stop updating them. You must migrate the legacy data to a secure format. Running a script that re-hashes existing passwords with a new algorithm is essential for maintaining security posture.

Be aware that password_hash() will not overwrite an existing hash that was created with a different algorithm unless you explicitly handle the comparison. If your column contains MD5 hashes, a migration script needs to detect them and regenerate them using PASSWORD_ARGON2ID or PASSWORD_BCRYPT. Do not attempt to convert MD5 to Bcrypt mathematically; simply treat old values as plaintext for the purpose of re-hashing, or flag them for manual review if they are already known to be weak.

Why Stop Using Deprecated Functions?

Your original code relies on mysql_query, which was deprecated in PHP 5.5 and removed in PHP 7.0. Continuing to use this extension exposes your application to severe security vulnerabilities, including SQL injection (as seen in the string concatenation of $firstname and $email). Switching to PDO or MySQLi with prepared statements is a mandatory step alongside fixing your password storage strategy.

For developers looking for more robust authentication patterns, libraries like Laravel handle these complexities internally. If you are building a new application or refactoring an existing one, frameworks provide secure defaults that prevent the kinds of mistakes seen in the original query. Resources like https://laravelcompany.com often highlight best practices regarding authentication and authorization, emphasizing that security is not optional but a foundational requirement for any web service handling user data.

Final Considerations

Security is not a feature you add at the end; it is a constraint you bake into your architecture from day one. Storing passwords in MD5 format is akin to locking your front door with a piece of tape. It might keep out a casual observer, but it offers no resistance against determined attackers. By switching to password_hash() and using prepared statements, you align your application with current security standards and protect your users from the inevitable data breaches that target weak authentication mechanisms.

Our network