PHP Code For Registration Form

PHP Code Snippets 2 Comments

PHP Code For Registration Form
Before you start writing PHP Code For Registration Form, you need to start with writing small HTML code for creating form elements, and styling them so that they look good.

Step 1 – Add HTML:

Create an HTML file, say index.html, and use an <form> element to process the input. Then add inputs (with a matching label) for each field:

<form action="submitform.php">
  <div class="container">
    <p>Please fill in this form to create an account.</p>

    <label for="email"><b>Email</b></label>
    <input type="text" placeholder="Enter Email" name="email" id="email" required>

    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="psw" id="psw" required>

    <label for="psw-repeat"><b>Repeat Password</b></label>
    <input type="password" placeholder="Repeat Password" name="psw-repeat" id="psw-repeat" required>
    <button type="submit" class="registerbtn">Register</button>
Step 2 – Add CSS

Add the below-mentioned code in the above HTML file so that the form element looks elegant.

* {box-sizing: border-box}

/* Add padding to containers */
.container {
  padding: 16px;

/* Full-width input fields */
input[type=text], input[type=password] {
  width: 100%;
  padding: 15px;
  margin: 5px 0 22px 0;
  display: inline-block;
  border: none;
  background: #f1f1f1;

input[type=text]:focus, input[type=password]:focus {
  background-color: #ddd;
  outline: none;

/* Overwrite default styles of hr */
hr {
  border: 1px solid #f1f1f1;
  margin-bottom: 25px;

/* Set a style for the submit/register button */
.registerbtn {
  background-color: #04AA6D;
  color: white;
  padding: 16px 20px;
  margin: 8px 0;
  border: none;
  cursor: pointer;
  width: 100%;
  opacity: 0.9;

.registerbtn:hover {

/* Add a blue text color to links */
a {
  color: dodgerblue;
Step 3 – submitform.php file code which inserts the data into the database.
//First of all, we'll have to include the database_connection.php file so that we can use the functions which we have defined in that file.
include 'database_connection.php';

// We'll connect with the database using StartDatabaseConnection() function. We are getting the result of calling this function in connectdb variable. If the connection to database becomes successful, this variable value will be true else it will be false.

//establish connection
$connectdb = StartDatabaseConnection();

//Code start for inserting the data in the database.
     $name = $_POST['name'];
     $email = $_POST['email'];
     $mobile = $_POST['mobile'];
     $sql = "INSERT INTO users (name,email,mobile)
     VALUES ('$name','$email','$mobile')";
     if (mysqli_query($connectdb, $sql)) {
        echo "New record has been inserted in the database successfully!";
     } else {
        echo "Error: " . $sql . ":-" . mysqli_error($connectdb);
else {
        echo "<br />"."Some issue.";

//Code ends for inserting the data into the database.

//close connection

Click the below link to check the demo. Download link to download the files used in this tutorial is also available on this page.

If you’re a freelancer, you can contact us here to send your resume for freelancing projects.

5 1 vote
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments

where is the database_connection.php code snip. Thank you a well explained procedure tho