Registration Form in PHP

Introduction

In modern web applications, one of the most common features is a registration form. Whether it’s a social media platform, an e-commerce site, or an online learning portal, users must first register before accessing advanced features.

A Registration Form in PHP allows users to create an account by submitting their personal details such as name, email, and password. PHP processes this data on the server side and stores it securely in a database like MySQL.

What is a Registration Form in PHP?

A Registration Form in PHP is a web form that collects user information and stores it in a database after proper validation and processing.

When a user fills out a registration form:

  1. Data is entered into input fields.
  2. The form is submitted to the server.
  3. PHP validates and sanitizes the data.
  4. The password is hashed for security.
  5. Data is stored in the database.

This process ensures that user information is securely saved and can later be used for login and authentication.

Syntax

1. Database Table Structure


CREATE TABLE users (
   id INT AUTO_INCREMENT PRIMARY KEY,
   name VARCHAR(100),
   email VARCHAR(100) UNIQUE,
   password VARCHAR(255),
   created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

2. HTML Registration Form


<form method="POST" action="register.php">
   <input type="text" name="name" placeholder="Enter Name" required>
   <input type="email" name="email" placeholder="Enter Email" required>
   <input type="password" name="password" placeholder="Enter Password" required>
   <button type="submit">Register</button>
</form>

3. PHP Registration Logic (register.php)


<?php
$conn = mysqli_connect("localhost", "root", "", "test_db");
$name = $_POST['name'];
$email = $_POST['email'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
// Check if email already exists
$check = "SELECT * FROM users WHERE email='$email'";
$result = mysqli_query($conn, $check);
if(mysqli_num_rows($result) > 0){
   echo "Email already registered!";
} else {
   $sql = "INSERT INTO users (name, email, password)
           VALUES ('$name', '$email', '$password')";
   if(mysqli_query($conn, $sql)){
       echo "Registration Successful";
   } else {
       echo "Error: " . mysqli_error($conn);
   }
}
?>

Example

Let’s create a simple working registration system step by step.

Step 1: Create Database

Create a database named test_db and a table users.

Step 2: Create Registration Form (register.html)


<!DOCTYPE html>
<html>
<head>
   <title>Register</title>
</head>
<body>

<form method="POST" action="register.php">
   <input type="text" name="name" placeholder="Enter Name" required><br><br>
   <input type="email" name="email" placeholder="Enter Email" required><br><br>
   <input type="password" name="password" placeholder="Enter Password" required><br><br>
   <button type="submit">Register</button>
</form>

</body>
</html>

Step 3: PHP File (register.php)


<?php
$conn = mysqli_connect("localhost", "root", "", "test_db");
if($_SERVER["REQUEST_METHOD"] == "POST"){
   $name = trim($_POST['name']);
   $email = trim($_POST['email']);
   $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
   // Insert data
   $sql = "INSERT INTO users (name, email, password)
           VALUES ('$name', '$email', '$password')";
   if(mysqli_query($conn, $sql)){
       echo "User Registered Successfully!";
   } else {
       echo "Error: " . mysqli_error($conn);
   }
}
?>

Step 4: Connect with Login System

Once users are registered, they can log in using their credentials through the login system.

Real-Life Example

Consider platforms like:

  • E-commerce websites (Amazon, Flipkart)
  • Social media (Facebook, Instagram)
  • Online learning platforms (Udemy, Coursera)

When a new user visits:

  1. They fill out a registration form.
  2. Their data is stored securely.
  3. They receive access to their account.

For example, on an online shopping site:

  • Users register to place orders.
  • Save delivery addresses.
  • Track their purchases.

Note: Without registration, these features would not be possible.

Common Mistakes

1. Not Hashing Password

❌ Storing plain text passwords is dangerous.
✔️ Always use:


password_hash($password, PASSWORD_DEFAULT);

2. No Input Validation

Not checking user input can lead to invalid or harmful data.

3. SQL Injection Risk

❌ Using direct queries:


$sql = "INSERT INTO users VALUES('$name','$email','$password')";

✔️ Use prepared statements instead.

4. Duplicate Email Entries

Not checking for existing emails can create duplicate accounts.

5. Weak Password Rules

Allowing short or simple passwords reduces security.

6. No Error Handling

Always display meaningful error messages.

Conclusion

A Registration Form in PHP is an essential component of any dynamic website. It enables users to create accounts and access personalized features. By combining HTML for the front-end and PHP for backend processing, developers can build secure and efficient registration systems.

However, security should always be a priority. Techniques like password hashing, input validation, and preventing SQL injection are crucial for protecting user data. Avoiding common mistakes and following best practices ensures a reliable system.

Related PHP Tutorials