Project 1 – Part 2: User Registration System in PHP

Introduction

In the previous tutorial, we introduced our PHP Login and Registration System project and prepared the database structure for storing user accounts.

In this part of the project, we will build the User Registration System using PHP and MySQL. A registration system allows new users to create accounts on a website by submitting their details through a form.

Many modern websites use registration systems to manage users. Examples include online forums, e-commerce stores, learning platforms, and social media websites.

By the end of this tutorial, users will be able to fill out a registration form, and their information will be securely stored in the database.

This step is an important part of building a complete authentication system.

What We Will Do in This Part

In this tutorial, we will create the user registration functionality of the login system.

The system will include the following features:

  • A registration form for collecting user details
  • Form validation using PHP
  • Password hashing for security
  • Secure database insertion using prepared statements
  • Success and error messages for users

Once this part is complete, users will be able to create accounts in the system.

Registration System Workflow

Before writing the code, it is important to understand how the registration process works.

The basic workflow is:

  1. The user fills out the registration form.
  2. The form data is sent to the server using the POST method.
  3. PHP validates the input fields.
  4. The password is encrypted using password hashing.
  5. The user data is inserted into the MySQL database.
  6. A success message is displayed.

This process allows websites to safely store user information and create new accounts.

Creating the Registration Form

First, create a file named:

register.php

Inside this file, create a simple HTML form.

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

<h2>User Registration</h2>

<form method="POST" action="">
    
Name:<br>
<input type="text" name="name" required><br><br>

Email:<br>
<input type="email" name="email" required><br><br>

Password:<br>
<input type="password" name="password" required><br><br>

<input type="submit" name="register" value="Register">

</form>

</body>
</html>

This form collects three pieces of information from the user:

  • Name
  • Email address
  • Password

When the user submits the form, the data will be processed by PHP.

Connecting to the Database

Next, include the database connection file that we created earlier.

Add this at the top of register.php.

<?php
include "db.php";
?>

This file contains the database connection code and allows the registration script to interact with the database.

Validating Form Data

Before storing user information in the database, we should first check whether the form was submitted.

Add the following PHP code below the database connection.

<?php
if(isset($_POST['register'])){
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];}
?>

This code checks whether the register button was clicked and retrieves the form data.

In real applications, you should also sanitize and validate the inputs to prevent invalid data.

Password Hashing for Security

Storing passwords as plain text is unsafe. Instead, we use password hashing.

PHP provides a built-in function called password_hash() to securely encrypt passwords.

Add the following code:

$hashed_password = password_hash($password, PASSWORD_DEFAULT);

This converts the user’s password into a secure encrypted format before saving it in the database.

Later, during login, the password will be verified using the password_verify() function.

Inserting Data into the Database

Now we will insert the user data into the users table.

To improve security, we use prepared statements.

$stmt = $conn->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $email, $hashed_password);

if($stmt->execute()){
    echo "Registration successful!";
}else{
    echo "Error: " . $stmt->error;
}

Complete Registration Code

Below is the complete register.php code.

<?php
include "db.php";if(isset($_POST['register'])){$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];$hashed_password = password_hash($password, PASSWORD_DEFAULT);$stmt = $conn->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $email, $hashed_password);if($stmt->execute()){
    echo "Registration successful!";
}else{
    echo "Error: " . $stmt->error;
}}
?>
<!DOCTYPE html>
<html>
<head>
<title>User Registration</title>
</head>
<body><h2>User Registration</h2><form method="POST">Name:<br>
<input type="text" name="name" required><br><br>Email:<br>
<input type="email" name="email" required><br><br>Password:<br>
<input type="password" name="password" required><br><br><input type="submit" name="register" value="Register"></form></body>
</html>

This script collects user input, hashes the password, and inserts the data into the database.

Testing the Registration System

To test the registration system:

  1. Start your local server (XAMPP or WAMP).
  2. Open the browser.
  3. Navigate to:
http://localhost/login-system/register.php
  1. Fill out the registration form.
  2. Submit the form.

If everything works correctly, the message “Registration successful!” will appear, and the new user will be added to the database.

You can confirm this by checking the users table in phpMyAdmin.

Common Beginner Mistakes

Beginners sometimes make mistakes when building registration systems.

Common mistakes include:

  • Forgetting to connect to the database
  • Not hashing passwords
  • Using incorrect table names
  • Not validating form inputs
  • Forgetting to check whether the form was submitted

Avoiding these mistakes will help your project work correctly.

Practice Tasks

Try the following tasks to practice what you learned.

  • Task 1: Create the register.php file.
  • Task 2: Build the registration form using HTML.
  • Task 3: Connect the file to the database using db.php.
  • Task 4: Insert user data into the database using prepared statements.
  • Task 5: Register a test user and confirm that the data appears in the users table.

Conclusion

In this tutorial, we built the User Registration System for our PHP login project. We created a registration form, validated user input, hashed passwords, and stored user data securely in the database.

A registration system is a fundamental part of many websites because it allows users to create accounts and interact with the platform.

In the next tutorial, we will build the Login System, where users will be able to log in using their email and password.

🚀 Make This Registration Form More Professional

Right now, when a user submits the registration form, the page reloads and then shows a message.

In modern web applications, we can improve this by:

  • Validating form inputs instantly using jQuery
  • Showing error messages without refreshing the page
  • Submitting the form using AJAX
  • Displaying success messages dynamically

To build this advanced version, you should first learn:

👉 Introduction to jQuery
👉 AJAX with PHP

Related Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *