How to Create a Registration Form in PHP and MySQL 2023

How to Create a Registration Form in PHP and MySQL
How to Create a Registration Form in PHP and MySQL

Share this Article!

Introduction

Registration forms are an essential component of any website that requires users to create accounts or sign up for a service. By creating a registration form in PHP and MySQL, you can easily collect user data and store it in a database. In this blog post, we will guide you through the steps, of how to create a registration form in PHP and MySQL.

User Registration Form PHP MySQL

Before we dive into the technical details, let’s talk about the importance of registration forms. Registration forms help website owners to collect user data such as name, email address, password, etc. This information can be used to personalize the user experience, send newsletters, and facilitate account recovery.

In addition, creating a registration form in PHP and MySQL allows you to store user data securely in a database. By doing so, you can easily retrieve user information, run analytics, and track user behavior. Now that we have established the importance of registration forms, let’s move on to the technical steps.

How to Create a Registration Form in PHP

Follow these steps to learn How to Create a Registration Form in PHP and MySQL in 2023.

  1. Setting up the Environment
  2. Create a new database in PHPMyAdmin on localhost
  3. Setting up the database
  4. Create a simple HTML form
  5. PHP validation and submission
  6. Storing user data in MySQL database
  7. Write PHP code for the user registration form
  8. Successful user registration confirmation

You can also watch the video tutorial for the step-by-step process of creating a new database on localhost. Also, learn How to Create a Registration Form in PHP and MySQL.

Video Tutorial – How to Create a Registration Form in PHP

Setting up the Environment

Before creating a registration form, you need to set up the environment. Start by installing XAMPP, which is an open-source platform that allows you to run PHP and MySQL locally on your computer. Once installed, create a new project and create a MySQL database.

How to Run PHP Code on Your Computer?

As you know PHP and MySQL are server-side languages. You need a web hosting live server or localhost to run PHP and Mysql codes. There are two ways to execute the PHP code on your computer to create a complete user registration and login system.

  • Buy a web hosting account. Host PHP files in your web hosting cPanel account inside the File Manager > public_html folder.
  • The second way is to make your computer a Local Server. Install an XAMPP application on your computer and place your PHP files inside the htdocs folder which is located at the following location on your computer.

C:\xampp\htdocs

After installation of XAMPP, you need to start the XAMPP control panel to run the PHP code on your local server. Start MySQL and Apache modules and wait for the green color indication to properly start the XAMPP local server.

How to Create a Registration Form in PHP
XAMP control panel window

Setting Up the Database

The first step in creating a user registration form is to set up a database to store user information. To do this, we’ll use MySQL, a popular relational database management system. Here’s a sample SQL query for creating a table to store user information:

CREATE TABLE users (
  id INT(11) AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(50) NOT NULL UNIQUE,
  email VARCHAR(50) NOT NULL UNIQUE,
  password CHAR(60) NOT NULL
);

This query creates a table called “users” with four columns: an auto-incrementing “id” column that serves as the primary key, a “username” column for storing the user’s chosen username, an “email” column for storing the user’s email address, and a “password” column for storing the user’s hashed password.

Video Guide – Setting up Database

Creating the HTML Form

The next step is to design the user interface of the registration form. You can use HTML to create the form and CSS to style it. Make sure that the form is user-friendly and easy to navigate.

To create the registration form, add input fields for user data such as name, email, password, etc. You can also add a checkbox for terms and conditions and a button to submit the form.

Here’s an example of what the HTML form might look like with source code:

<?php include('register.php'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<title>User Registration Form</title>
	<style>
		body {
			background-color: #eeeeee;
			font-family: Arial, sans-serif;
			padding: 80px;
		}
		form {
			background-color: #fff;
			border-radius: 5px;
			box-shadow: 0px 0px 10px #ccc;
			padding: 20px;
			width: 350px;
			margin: 0 auto;
		}
		input[type=text],
		input[type=password],
		input[type=email] {
			width: 100%;
			padding: 12px 20px;
			margin: 8px 0;
			display: inline-block;
			border: 1px solid #ccc;
			border-radius: 4px;
			box-sizing: border-box;
		}
		button[type=submit] {
			background-color: #4CAF50;
			color: white;
			padding: 14px 20px;
			margin: 8px 0;
			border: none;
			border-radius: 4px;
			cursor: pointer;
			width: 100%;
		}
		button[type=submit]:hover {
			background-color: #45a049;
		}
		#successMessage {
			color: green;
			font-size: 15px;
			margin: 10px;
		}
		#errorMessage {
			color: red;
			font-size: 15px;
			margin: 10px;
		}
	</style>
</head>
<body>
	<form method="POST">
		<h2>User Registration Form</h2>
		<label for="username">Username:</label>
		<input type="text" id="username" name="username" required>
		<label for="email">Email:</label>
		<input type="email" id="email" name="email" required>
		<label for="password">Password:</label>
		<input type="password" id="password" name="password" required>
		<!-- Display success and error messages -->
		<div id="errorMessage"> <?php echo $error  ?> </div>
		<div id="successMessage"> <?php echo $success  ?> </div>
		<button type="submit" name="button">Register</button>
	</form>
</body>
</html>

This form includes three input fields for the user’s chosen username, email address, and password. We’ve also added the “required” attribute to each input field to ensure that the user fills in all required information before submitting the form.

Form OutPut

How to Create a Registration Form in PHP
User Registration Form

PHP Validation and Submission

Now that we have a form that collects user information, we need to create a PHP script that validates the submitted data and inserts it into the MySQL database.

Validating User Input

After designing the form, it’s important to validate user input. Server-side validation ensures that user input is correct and meets the required criteria. In PHP, you can use the “filter_input()” function to validate user input.

PHP code for validating user input should be added before inserting the data into the database. You can also display error messages to guide users in filling out the form correctly.

Storing User Data in MySQL

Once the user input is validated, the next step is to store user data in a MySQL database. To do this, create a PHP file to connect to MySQL and insert user data into the database. You can use the “mysqli” function in PHP to connect to the MySQL database.

User Registration – PHP Code

Here’s an example of what the PHP script might look like: Create a new PHP file name register.php and paste the below code inside this.

<?php
// Connect to the MySQL database
$success = "";
$error = "";
if (array_key_exists("button", $_POST)) {
$mysqli = new mysqli("localhost", "root", "", " Your Database name");
// Check for errors
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
} else { 
  // Validate the submitted data
  $username = $_POST["username"];
  $email = $_POST["email"];
  $password = password_hash($_POST["password"], PASSWORD_DEFAULT);
  // Check if the username or email already exists in the database
  $result = $mysqli->query("SELECT * FROM users WHERE username='$username' OR email='$email'");
  if ($result->num_rows > 0) {
      $error = "Username or email already taken.";
  } else {
    // Insert the user's information into the database
    $mysqli->query("INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')");
    
    //success message
      $success = "Data submitted Successfully";
  }
}
}
?>

Connect To MySQL Using PHP

Specify MySQL server name, username, password, and database name parameters in your PHP code. Here, my server name is localhost, MySQL username is root and its password is empty. Create a connection using the above details.

Code:

$connection = mysqli_connect("server_name", "user_name", "password", "database");

Check the connection

We can check the connection using the mysqli_connect_error() function specified in an if condition. This function will display an error if the connection is failed.

This PHP script first connects to the MySQL database using the mysqli class. It then checks for errors and validates the submitted data by hashing the user’s password using the password_hash function. The script then checks if the submitted username or email address already exists in the database.

Successful Registration Confirmation

After successfully inserting user data into the MySQL database, display a success message to confirm that the user has been registered. You can also redirect users to a login page.

Conclusion

In this blog post, we have shown you how to create a registration form in PHP and MySQL. Following these steps, you can easily collect user data and store it securely in a database. Registration forms are an essential component of any website that requires users to create accounts or sign up for a service. So, start creating your registration form today and start collecting user data!

Share this Article!

Leave a Reply

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

Subscribe Our YouTube Channel!

Follow for Latest Updates