Skip to main content

Creating a User Registration Form

In the previous tutorial, you created a users table and learned how to securely store passwords using password hashing. In this tutorial, you will create a registration form that allows new users to create an account and store their details in the database.


Create the Registration Form

Create a new file called:

register.php

Add the following code:

<!DOCTYPE html>
<html>
<head>
    <title>User Registration</title>
</head>
<body>

<h1>Create Account</h1>

<form action="register.php" method="post">

    <label>Username</label><br>
    <input type="text" name="username" required><br><br>

    <label>Password</label><br>
    <input type="password" name="password" required><br><br>

    <button type="submit">Register</button>

</form>

</body>
</html>

Save the file and open it in your browser.

Example:

http://localhost/register.php

You should see a simple registration form.

Connect to the Database

Add the following PHP code immediately before the <!DOCTYPE html> line:

<?php

$conn = new mysqli(
    "localhost",
    "root",
    "",
    "project_db"
);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

?>

This creates a connection to the project_db database.

Process the Form Submission

Add the following code underneath the database connection:

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $username = $_POST["username"];
    $password = $_POST["password"];

}

This code runs when the form is submitted.


Hash the Password

Inside the if statement, add:

$hashedPassword = password_hash(
    $password,
    PASSWORD_DEFAULT
);

Your code should now look like:

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $username = $_POST["username"];
    $password = $_POST["password"];

    $hashedPassword = password_hash(
        $password,
        PASSWORD_DEFAULT
    );

}

The password will now be securely hashed before being stored.


Insert the User into the Database

Add the following code underneath the password hashing:

$stmt = $conn->prepare(
    "INSERT INTO users (username, password)
     VALUES (?, ?)"
);

$stmt->bind_param(
    "ss",
    $username,
    $hashedPassword
);

$stmt->execute();

This inserts the username and hashed password into the users table.


Display a Success Message

Add:

echo "<p>Account created successfully.</p>";

The completed section should look like:

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $username = $_POST["username"];
    $password = $_POST["password"];

    $hashedPassword = password_hash(
        $password,
        PASSWORD_DEFAULT
    );

    $stmt = $conn->prepare(
        "INSERT INTO users (username, password)
         VALUES (?, ?)"
    );

    $stmt->bind_param(
        "ss",
        $username,
        $hashedPassword
    );

    $stmt->execute();

    echo "<p>Account created successfully.</p>";
}

Create a New User Account

Open:

http://localhost/register.php

Enter:

Username: testuser
Password: mypassword

Click Register.

You should see:

Account created successfully.

Check the Database

Open phpMyAdmin and view the users table.

Run:

SELECT * FROM users;

You should now see the new account.

Example:

user_id username password
1 admin $2y$10$...
2 teacher $2y$10$...
3 testuser $2y$10$...

Notice that the password is stored as a hash rather than plain text.

Complete Code

<?php

$conn = new mysqli(
    "localhost",
    "root",
    "",
    "project_db"
);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $username = $_POST["username"];
    $password = $_POST["password"];

    $hashedPassword = password_hash(
        $password,
        PASSWORD_DEFAULT
    );

    $stmt = $conn->prepare(
        "INSERT INTO users (username, password)
         VALUES (?, ?)"
    );

    $stmt->bind_param(
        "ss",
        $username,
        $hashedPassword
    );

    $stmt->execute();

    echo "<p>Account created successfully.</p>";
}

?>

<!DOCTYPE html>
<html>
<head>
    <title>User Registration</title>
</head>
<body>

<h1>Create Account</h1>

<form action="register.php" method="post">

    <label>Username</label><br>
    <input type="text" name="username" required><br><br>

    <label>Password</label><br>
    <input type="password" name="password" required><br><br>

    <button type="submit">Register</button>

</form>

</body>
</html>

You now have a working registration form that stores user accounts in the database using secure password hashing.

Next tutorial: Creating a Login Form.