How to Insert Password Hash PHP in Mysql I XAMPP

How to Insert Password Hash PHP in Mysql I XAMPP

1. Fixed & Improved Login Form (HTML)

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Register</title> <link rel="stylesheet" type="text/css" href="Login.css"> </head> <body> <div class="loginbox"> <img src="Logo1.png" class="avatar" alt="Logo"> <form method="post" action="insertpassword.php"> <h1>Register Here</h1> <label for="username">Username</label> <input type="text" id="username" name="username" placeholder="Enter Username" required> <label for="password">Password</label> <input type="password" id="password" name="password" placeholder="Enter Password" required> <label for="repassword">Confirm Password</label> <input type="password" id="repassword" name="repassword" placeholder="Re-enter Password" required> <input type="submit" name="save" value="Sign Up"> </form> </div> </body> </html>

2. Secure User Registration (PHP)

<?php $conn = new mysqli("localhost", "root", "", "loginphp"); if ($conn->connect_error) { die("Database Error: " . $conn->connect_error); } mysqli_set_charset($conn, "utf8"); if ($_SERVER["REQUEST_METHOD"] == "POST") { if (isset($_POST["username"], $_POST["password"], $_POST["repassword"])) { $username = trim($_POST["username"]); $password = trim($_POST["password"]); $repassword = trim($_POST["repassword"]); // Check if passwords match if ($password !== $repassword) { die("Passwords do not match."); } // Hash the password $hashed_password = password_hash($password, PASSWORD_DEFAULT); // Prevent SQL Injection $stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)"); $stmt->bind_param("ss", $username, $hashed_password); if ($stmt->execute()) { echo "Successfully registered. <a href='login.html'>Login here</a>"; } else { echo "Error: " . $stmt->error; } $stmt->close(); } else { echo "All fields are required!"; } } $conn->close(); ?>

Fixes & Enhancements

Fixed SQL Query Errors:

  • musqli_error($conn); → corrected as mysqli_error($conn);

  • INSERT INTO loginphp → changed to INSERT INTO users (username, password) VALUES (?, ?)

Improved Security:

  • Used password hashing (password_hash()) for security.

  • Used prepared statements (mysqli_prepare()) to prevent SQL injection.

Bug Fixes:

  • Ensured all fields were properly checked before inserting into the database.

  • Redirects users to the login page after successful registration.

Would you like me to add login validation or extra security features like email verification? 

https://drive.google.com/file/d/1DwHIjw8SL0ICLFbEcik4W41bwHjOayAc/view
Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close