VOOZH about

URL: https://www.geeksforgeeks.org/html/how-to-match-username-and-password-in-database-in-sql/

⇱ How to match username and password in database in SQL ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to match username and password in database in SQL ?

Last Updated : 23 Jul, 2025

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.

👁 Image

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 function is used to connect the database.

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'

👁 Image

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:

Output:

👁 Image

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

👁 Image

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:

Output:

👁 Image

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

👁 Image

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.

Output:

👁 Image

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:       

Comment