Open In App

How to match username and password in database in SQL ?

Last Updated : 02 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to check the credentials (username and password) entered by the user. If credentials are matched from the database entries, the user can enter into another page and if credentials are incorrect then it displays “Sorry Invalid Username and Password”.

Pre-requisites: XAMPP server, Basics of HTML, CSS, PHP,  MySQL.

Step 1: First, we must start the XAMPP server from the XAMPP control panel. XAMPP is bundled software.

Open XAMPP Control Panel and start Apache and MySQL services. In the XAMPP folder, go to htdocs folder and create a folder named check_username_pwd. We will keep all the files in the project folder. The files are index.php, login.php, check.php.            

Step 2: Open the editor of your choice. Create a file named connect.php to make the database connection. The connection object is returned to the $conn variable. The name of the database is check.

connect.php: The mysqli_connect() function is used to connect the database.

PHP




<?php
$conn = mysqli_connect("localhost", "root", "", "check");
?>


Step 3: Create a database table.

  • Go on localhost/phpMyAdmin
  • Create a database with a name check.
  • Now click on SQL and write the query code mentioned below.
CREATE TABLE register(
    id int(10) AUTO_INCREMENT PRIMARY KEY,
    firstname varchar(50),
    lastname varchar(50),
    username varchar(100),
    year varchar(20),
    mobile varchar(20),
    password varchar(50)
);

A Database table is created with the name ‘register

Step 4: Create another file named index.php

This page is a PHP form that is used to insert a user’s data to create a username and password. When data is inserted, an alert message “You Registered Successfully” is shown. This page contains a hyperlink login, on clicking on this user jumps to the login.php.

index.php:

PHP




<?php
  
include("connect.php");
  
if(isset($_POST['submit'])) {
    $query = mysqli_query($conn,
    "INSERT INTO REGISTER SET firstname='"
    . $_POST["firstname"] . "'  ,lastname ='"
    . $_POST["lastname"] . "'   ,username ='"
    . $_POST["username"] . "'    ,year     ='"
    . $_POST["year"] . "'        ,mobile     ='"
    . $_POST["mob"] . "'        ,password ='"
    . $_POST["pwd"] . "'        ");
      
?>
<script>
    alert('You Registered Successfully, Login now');
</script>
<?php
}
?>
<html>
 
<head>
    <meta charset="utf-8">
    <title>Register Page</title>
    <style>
        th {
            text-align: left;
        }
 
        td {
            text-align: center;
        }
 
        a {
            text-decoration: none;
        }
    </style>
</head>
 
<body>
    <a href="login.php"
        style="font-size:30px; float:right;">
        Login
    </a>
    <form method="post" action="index.php" name="frm1">
 
        <fieldset>
            <legend align="center">
                <h1>Register Here</h1>
            </legend>
            <table align="center" border="1"
                width="40%" style="border:thick;">
                <tr>
                    <th height="40"><label for="firstname">
                            First Name</label>
                    </th>
                    <td><input type="text"
                        name="firstname"
                        id="firstname" required>
                    </td>
                </tr>
                <tr>
                    <th height="40"><label for="lastname">
                            Last Name</label>
                    </th>
                    <td><input type="text"
                            name="lastname"
                            id="lastname" required>
                    </td>
                </tr>
                <tr>
                    <th height="40"><label for="username">
                            Username</label>
                    </th>
                    <td><input type="text"
                        name="username"
                        id="username" required>
                    </td>
                </tr>
                <tr>
                    <th height="40">
                        <label for="year">Year</label>
                    </th>
                    <td><select name="year"
                        id="year" required>
                            <option value="">
                                Choose Year
                            </option>
                            <option value="First Year">
                                First Year
                            </option>
                            <option value="Second Year">
                                Second Year
                            </option>
                            <option value="Third Year">
                                Third Year
                            </option>
                            <option value="Fourth Year">
                                Fourth Year
                            </option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th height="40">
                        <label for="mob">Mob.No.</label>
                    </th>
                    <td><input type="tel" name="mob"
                            id="mob" required>
                    </td>
                </tr>
                <tr>
                    <th height="40">
                        <label for="pwd">Password</label>
                    </th>
                    <td><input type="password"
                        name="pwd" id="pwd" required>
                    </td>
                </tr>
                <tr>
                    <td height="40" colspan="2"><input
                        type="submit" name="submit"
                        value="Register">
                    </td>
                </tr>
            </table>
        </fieldset>
    </form>
</body>
 
</html>


Output:

This is the output of the database after entering data through the registration form which is available on index.php.

Step 5: Create another file login.php. In this, we create a login form. When a user enters their username and password and if it is correct user jump on another page check.php.

login.php:

PHP




<?php
    
include("connect.php");
 
if(isset($_POST['login'])) {
    $sql = mysqli_query($conn,
    "SELECT * FROM REGISTER WHERE username='"
    . $_POST["username"] . "' AND
    password='" . $_POST["pwd"] . "'    ");
   
    $num = mysqli_num_rows($sql);
   
    if($num > 0) {
        $row = mysqli_fetch_array($sql);
        header("location:check.php");
        exit();
    }
    else {
?>
<hr>
<font color="red"><b>
        <h3>Sorry Invalid Username and Password<br>
            Please Enter Correct Credentials</br></h3>
    </b>
</font>
<hr>
 
<?php
      }
}
?>
<html>
 
<head>
    <style>
        th {
            text-align: left;
        }
 
        td {
            text-align: center;
        }
 
        a {
            text-decoration: none;
        }
    </style>
</head>
 
<body>
    <form method="post" action="login.php">
        <fieldset>
            <legend align="center">
                <h1 align="center">Login</h1>
            </legend>
            <table width="50%" border="1"
                align="center" style="border:thick;">
                <tr>
                    <th height="40"><label for="username">
                        Username</label>
                    </th>
                    <td><input type="text" name="username"
                            id="username" required>
                        </td>
                </tr>
                <tr>
                    <th height="40"><label for="pwd">
                        Password
                    </label>
                    </th>
                    <td><input type="password"
                        name="pwd" id="pwd" required></td>
                </tr>
                <tr>
                    <td colspan="2" height="40"><input
                        type="submit" name="login"
                        value="Login"></td>
                </tr>
            </table>
        </fieldset>
    </form>
</body>
 
</html>


Output:

 

And if the username and password are not matched then it shows  “Sorry Invalid Username and Password”.

Step 6: Create another file check.php. This page is opened when the user enters the correct credentials and then clicks on the login button that means the Username and Password matched from the database.

PHP




<html>
 
<body>
    <h2>
        Your login Credientials i.e.;
        Username, Password are matched
    </h2>
</body>
 
</html>


Output:

Step 6: Run this project in Browser.

  • Open browser(Google Chrome)
  • Write the proper path of the project in the URL bar “localhost/check_username_pwd/”

Output:       



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads