Skip to main content

How to Create a simple Registration Form in PHP/MySQLi and HTML

Language

Introduction:

This tutorial will cover creating a register form with PHP/MySQLi/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.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Simpe Registration</title>
  6. </head>
  7. <body align="middle">
  8. <div><center><h2>Registration Form.</h2></center></div>
  9. <form action='register.php' method='POST'>
  10. <label for="user">Username:</label> <br/>
  11. <input type='text' name='user' id="user" required/><br/>
  12. <label for="user">Password:</label> <br/>
  13. <input type='password' name='pass' id="pass" required/><br/>
  14. <label for="user">Email:</label> <br/>
  15. <input type='email' name='email' id="email" required/><br/>
  16. <input type='submit' name='sentForm' id="sentForm" />
  17. </form>
  18. </body>
  19. </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.

  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] == 'POST' && isSet($_POST['sentForm'])) {
  3. $conn = mysqli_connect('localhost', 'root', '','tutorial') or die("Connection failed: " . mysqli_connect_error());
  4. if (isSet($_POST['user']) && isSet($_POST['email']) && isSet($_POST['pass'])) {
  5. $user = $_POST['user'];
  6. $pass = $_POST['pass'];
  7. $email = $_POST['email'];
  8.  
  9. $sql = "INSERT INTO `users` (`username`,`password`,`email`) VALUES ('$user', '$pass', '$email')";
  10.  
  11. $query = mysqli_query($conn,$sql);
  12. if ($query) {
  13. echo 'Data Successfullu Saved!';
  14. } else {
  15. echo "An error occured while save the data.";
  16. }
  17. }
  18. }
  19. ?>

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.

Please note:

This form does not have any security in place, I will cover the security in another tutorial and place the link to that tutorial here once it has been uploaded.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Add new comment