PHP - Validate Username Availability

In this tutorial we will create a Validate Username Availability using PHP. This code can validate the given username if it is available to register to the MySQLi database server. The code use PHP POST to initiate a method that validate the username input if it is available to be register to database using MySQLi SELECT by adding a WHERE clause between `username` and the input value. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language that interpret in the webserver such as xamp, wamp, etc. It is widely use by modern website application to handle and protect user confidential information.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_valid, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = mysqli_connect('localhost', 'root', '', 'db_valid');
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Validate Username Availability</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-5">
  18. <form method="POST" action="">
  19. <div class="form-group">
  20. <label>Username:</label>
  21. <input type="text" name="username" class="form-control" required="required"/>
  22. <?php include'validate.php'?>
  23. </div>
  24.  
  25. <div class="form-group">
  26. <label>Password:</label>
  27. <input type="password" name="password" class="form-control" required="required"/>
  28. </div>
  29.  
  30. <div class="form-group">
  31. <label>Firstname:</label>
  32. <input type="text" name="firstname" class="form-control" required="required"/>
  33. </div>
  34.  
  35. <div class="form-group">
  36. <label>Lastname:</label>
  37. <input type="text" name="lastname" class="form-control" required="required"/>
  38. </div>
  39.  
  40. <div class="form-group">
  41. <center><button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Register</button></center>
  42. </div>
  43. </form>
  44. </div>
  45. </div>
  46. </body>
  47. </html>

Creating Main Function

This code contains the main function of the application. This code will validate the username when the button is clicked. To do that just copy and write this block of codes inside the text editor, then save it as validate.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $username = $_POST['username'];
  6. $password = $_POST['password'];
  7. $firstname = $_POST['firstname'];
  8. $lastname = $_POST['lastname'];
  9.  
  10. $query = $conn->query("SELECT * FROM `user` WHERE `username` = '$username'");
  11. $rows = $query->num_rows;
  12.  
  13. if($rows > 0){
  14. echo "<span class='text-danger'>Username is not available</span>";
  15. }
  16.  
  17. mysqli_query($conn, "INSERT INTO `user` VALUES('', '$username', '$password', '$firstname', '$lastname')") or die(mysqli_error());
  18.  
  19. }
  20. ?>
There you have it we successfully created Validate Username Availability using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Comments

Add new comment