PHP

PHP Registration Form

A PHP Registration Form takes input from the user and saves the information in the database.

You can see this form in every web and android application.

The purpose of using the Registration form on the websites is to make users able to access the website’s information easily.

If the user is registered on a website or application so he or she will get a new account, and the user will use that account for logging in to that website or application and can access the important information.  

Let’s see how to create a PHP Registration Form.

Step 1 – HTML Code

First, we will create a simple HTML Registration Form

<!DOCTYPE html>
<html>
<head>
  <title>PHP Registration form</title>
</head>
<body>
<h1 style="margin-top: 5%; text-align: center; color: black">Registration Form</h1>
<form action="myyy.php" method="post">
<h3 style="margin-top: 3%; margin-left: 25%">Name</h3>
  <input type="text" name="name" style="height: 5vh; width: 45%;
  border-radius: 2px black; margin-top: 0.5%; margin-left: 25%; " required="required">
<h3 style="margin-top: 2%; margin-left: 25%">Email</h3>
  <input type="Email" name="email" style="height: 5vh; width: 45%;
  border-radius: 2px black;  margin-top: 0.5%; margin-left: 25%" required="required">

<h3 style="margin-top: 2%; margin-left: 25%">Password</h3>
  <input type="Password" name="pass" style="height: 5vh; width: 45%;
  border-radius: 2px black; margin-top: 0.5%; margin-left: 25%" required="required">  
  <br>
  <input type="submit" name="sub" style="height: 5vh; width: 10;
  border-radius: 2px black;margin-top: 2%; margin-left: 25%">
</form>
</body>
</html> 

Code Explanation

In the above HTML code, we created a Registration form using the HTML form tag.

In the form, we took four input fields for the user’s name, email, and password and the fourth one is for submitting the form when the user clicks on it, the user information will be stored in the database and the user will be registered.

We used inline CSS for designing the simple registration form. The Registration form will look like this.

Step 2 – PHP

Now we will add PHP code for connecting the file with the database and storing the user information.

<?php
if (isset($_POST['sub'])) {
  $name=$_POST['name'];
$email=$_POST['email'];
  $pw=$_POST['pass'];
$conn=mysqli_connect("localhost","root","","intern");
$q="INSERT INTO userin (`name`,`email`,`password`) VALUES ('$name','$email','$pw')";
mysqli_query($conn,$q);
}
?>      

Code Explanation

In the above PHP code, we used the isset function with the if condition.

And then we declare three variables and assign them the user input values after that, 

We made a connection with the database using the mysqli_connect() function and then

We wrote a query that will insert the user input values into the database table.

The isset function will check if the user clicked on submit button or not, if it is clicked so the

User input data will be stored in the database, which means username, email, and password will be saved in the database and the user will be registered, and then the user can easily log in to his or her account. 

The whole PHP Registration Form code is below.

PHP Registration Form (Complete Code)

<?php
if (isset($_POST['sub'])) {
  $name=$_POST['name'];
$email=$_POST['email'];
  $pw=$_POST['pass'];
 
$conn=mysqli_connect("localhost","root","","intern");
$q="INSERT INTO userin (`name`,`email`,`password`) VALUES ('$name','$email','$pw')";
mysqli_query($conn,$q);
}
?>
<!DOCTYPE html>
<html>
<head>
  <title>PHP Registration form</title>
</head>
<body>
<h1 style="margin-top: 5%; text-align: center; color: black">Registration Form</h1>
<form action="myyy.php" method="post">
<h3 style="margin-top: 3%; margin-left: 25%">Name</h3>
  <input type="text" name="name" style="height: 5vh; width: 45%;
  border-radius: 2px black; margin-top: 0.5%; margin-left: 25%; " required="required">
<h3 style="margin-top: 2%; margin-left: 25%">Email</h3>
  <input type="Email" name="email" style="height: 5vh; width: 45%;
  border-radius: 2px black;  margin-top: 0.5%; margin-left: 25%" required="required">
<h3 style="margin-top: 2%; margin-left: 25%">Password</h3>
  <input type="Password" name="pass" style="height: 5vh; width: 45%;
  border-radius: 2px black; margin-top: 0.5%; margin-left: 25%" required="required">  
  <br>
  <input type="submit" name="sub" style="height: 5vh; width: 10;
  border-radius: 2px black;margin-top: 2%; margin-left: 25%">
</form>
</body>
</html>

Conclusion

In this tutorial we discussed what is a registration form and see the making of it, we discuss the HTML and PHP code for creating a Registration form.

Leave a Comment