How To Reset Password by Email Using PHP7 and MySQLi —

Parvez Alam
2 min readAug 8, 2021

You will learn How to recover/reset a forgotten password using PHP and MySQLi in this post. We’ll show you how to use a Forgot Password form to recover your password if you’ve forgotten your username or email. We’ll send a password recovery email to the user after the form is submitted.

There are several accounts on various websites and platforms. The majority of individuals forget their passwords and attempt to reset their passwords on the website.

In this post, You’ll learn how to use PHP and MySQLi to retrieve a forgotten password.

Recover Forgot Password By Email Using PHP7 and MySQLi

We will create a forgot password HTML form using bootstrap 4. There are three fields in this file: username, new password, and confirm the new password, as well as a Submit button. This form is forwarded to the forgot.php page after the user submits the form.

Assume the database contains a 'users' TABLE with one user entry, as shown below. If you have a database, you can use it.

Connect MySQL Database with PHP Using mysqli_connect() :

Let’s create a connect.php file to connect MySQL database with PHP. The below code is to connecting database and selecting the database.

Create a Form for Forgot Password

Created a forgot.php file into the PHP project. We have started the session at the top of the file. Let's add the HTML code that shows the forgot password form -

In the code above, we have created one email field into the form. The user will enter the register email id and ll receive an email once the emailid is correct.

PHP Code to Send Forgotten Password by E-Mail

Once this forgot password form is submitted, the entered data is sent in the post to the 'handler.php' file.Let's create a handler.php php script that will get form data is POST method.

in the above code, The First check the POST superglobal is set & not empty and assign the submitted emailid to a variable. Check that username exists in the database using the select query.

If the mysqli_num_rows() PHP function returns one row, then we will send an email with a plain text password that is fetched from the database using the select query. Assign values of email, password to variables fetched from the select query.

Send Email Using PHP

Let’s create a PHP logic that will send mail using PHPMailer class.


We have connected the MySQLi database with PHP using mysqli_connect() method. We have also created forgot password form using bootstrap 4.Also send forgot email using PHP mailer.

Originally published at on August 8, 2021.



Parvez Alam

Hey, I am Parvez Alam. A software developer since 2009. I love learning and sharing knowledge.