How to Create a simple Registration Form using PHP/MySQL and HTML

How to Create a simple Registration Form using PHP/MySQL and HTML

This tutorial will cover creating a register form with PHP/MySQL/HTML.

Steps of Creation:

Step 1:

First, we need to create a database to hold the user accounts. This is done through MySQL/PHPMyAdmin.

On your localhost/web host cPanel/Control Panel go to MySQL Databases and create a new one (for example; the name of your site – Mine will be “tutorial”). Next, go to PHPMyAdmin and select the created database.

Once you are on the database in PHPMyAdmin, create a table to hold the data – in this case, it’s our users’ accounts. Let’s call it “users”.

We want four columns:

[Name – Type – Length – Additionals…]

  • id – int – 5 – Auto Increment (A_I/AI) – Primary Key
  • username – varchar – 250
  • password – varchar – 250
  • email – varchar – 250

Click create and now we are ready to insert the data.

Step 2:

Now we can create a register form in HTML for the users. Create an index.php file, then copy and paste the code below.

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Simpe Registration</title>
</head>
<body align="middle">
 <div><center><h2>Registration Form.</h2></center></div>
 <form action='register.php' method='POST'>
  <label for="user">Username:</label> <br/>
  <input type='text' name='user' id="user"  required/><br/>
  <label for="user">Password:</label> <br/>
  <input type='password' name='pass' id="pass"  required/><br/>
  <label for="user">Email:</label> <br/>
  <input type='email' name='email' id="email"  required/><br/>
  <input type='submit' name='sentForm' id="sentForm" />
 </form>
</body>
</html>

As you can see, the method of the form is POST which means the data is sent from the form invisible to the average user and the action is ‘register.php’. The “required” attribute in each inputs will help to validate the form to prevent sending a null/blank values.

Step 3:

The below script is our “register.php” file.

<?php
 if ($_SERVER['REQUEST_METHOD'] == 'POST' && isSet($_POST['sentForm'])) {
  $conn = mysqli_connect('localhost', 'root', '','tutorial') or die("Connection failed: " . mysqli_connect_error());
  if (isSet($_POST['user']) && isSet($_POST['email']) && isSet($_POST['pass'])) {
   $user = $_POST['user'];
   $pass = $_POST['pass'];
   $email = $_POST['email'];
 
   $sql = "INSERT INTO `users` (`username`,`password`,`email`) VALUES ('$user', '$pass', '$email')";
 
   $query = mysqli_query($conn,$sql);
   if ($query) {
    echo 'Data Successfullu Saved!';
   } else {
    echo "An error occured while save the data.";
   }
  }
 }
?>

As you can see, we checked first if the page data is using the “POST” method and “sentForm” button. The above script connects to the database with the given information (server_name, username, password,database_name). Then we get the information from the form through our POST data and insert them into the ‘users’ table in our database as a new row. Placing a value for our “id” column is not necessary since we have already set it into “Auto Increment” value.

Testing

Once you have finished, it should all be working. Try to create an account and check the database to see if the account/row exists.

Dheeraj Pal:

This website uses cookies.