# 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:

```text
register.php
```

Add the following code:

```php
<!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:

```text
http://localhost/register.php
```

You should see a simple registration form.

[![](https://mr.napper.au/uploads/images/gallery/2026-06/scaled-1680-/image-1780893823644.png)](https://mr.napper.au/uploads/images/gallery/2026-06/image-1780893823644.png)

## Connect to the Database

Add the following PHP code immediately before the `<!DOCTYPE html>` line:

```php
<?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:

```php
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:

```php
$hashedPassword = password_hash(
    $password,
    PASSWORD_DEFAULT
);
```

Your code should now look like:

```php
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:

```php
$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:

```php
echo "<p>Account created successfully.</p>";
```

The completed section should look like:

```php
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:

```text
http://localhost/register.php
```

Enter:

```text
Username: testuser
Password: mypassword
```

Click **Register**.

You should see:

```text
Account created successfully.
```



## Check the Database

Open phpMyAdmin and view the users table.

Run:

```sql
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.

[![](https://mr.napper.au/uploads/images/gallery/2026-06/scaled-1680-/image-1780894149294.png)](https://mr.napper.au/uploads/images/gallery/2026-06/image-1780894149294.png)

## Complete Code

```php
<?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**.