Download Source Code -PHP – Validate Username Availability

Loading...

PHP – Validate Username Availability

Submitted by: 
razormist
Language: 
PHP
Visitors have accessed this post 7 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Validate Username Availability</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-5">
  19. <form method="POST" action="">
  20. <div class="form-group">
  21. <label>Username:</label>
  22. <input type="text" name="username" class="form-control" required="required"/>
  23. <?php include'validate.php'?>
  24. </div>
  25.  
  26. <div class="form-group">
  27. <label>Password:</label>
  28. <input type="password" name="password" class="form-control" required="required"/>
  29. </div>
  30.  
  31. <div class="form-group">
  32. <label>Firstname:</label>
  33. <input type="text" name="firstname" class="form-control" required="required"/>
  34. </div>
  35.  
  36. <div class="form-group">
  37. <label>Lastname:</label>
  38. <input type="text" name="lastname" class="form-control" required="required"/>
  39. </div>
  40.  
  41. <div class="form-group">
  42. <center><button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Register</button></center>
  43. </div>
  44. </form>
  45. </div>
  46. </div>
  47. </body>
  48. </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!

Tags: 
php
MySQLi
Username
Bootstrap
validate

Add new comment

(Visited 3 times, 1 visits today)
Loading...