In this post I will explain you how user can change their old password. First create a HTML Form with three fields. It\’s full validate form like old password and new or confirm password match or not.
File Structure for this tutorial:
- Create Change Password HTML Form
- Create Database Connection with phpmyadmin
- Create a class file here the change function on this class file.
See Also
- Create User Registration and Login using PHP OOP Concepts
- PHP Login Script with Remember Me using Cookies
- PHP Login Registration Script by using Password_hash() & Password verify() method for Secure login
- Sign Up and Login using jQuery AJAX in PHP Mysql
Create Change Password HTML Form
change_password.php
<!DOCTYPE html> <html lang="en"> <head> <title>Change Password in PHP with Mysql using OOPs concepts</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> </head> <?php include_once "class/students.php"; $studentObj = new Students(); $success = ""; $error = ""; if (isset($_POST['submit'])) { $newdata['new_password'] = md5($_POST['new_password']); $newdata['old_password'] = md5($_POST['old_password']); $newdata['con_password'] = md5($_POST['con_password']); $id = $_SESSION['id']; if ($newdata['new_password'] !== "" && $newdata['old_password'] !== "" && $newdata['con_password'] !== "") { if ($newdata['new_password'] == $newdata['con_password']) { if ($studentObj->changePassword($id)) { $success = "Your new password update successfully."; }else{ $error = "Old Password is not match"; } }else{ $error = "New password and confirm password do not matched"; } }else{ $error = "Please fill the all fields"; } } ?> <body class="hold-transition login-page"> <div class="login-box"> <div class="login-logo"> <a href=""><b>Change Password</b></a> </div> <?php if (!empty($error)) { echo "<div class='alert alert-danger alert-dismissible'> <button type='button' class='close' data-dismiss='alert'>×</button>$error </div>"; } elseif (!empty($success)) { echo "<div class='alert alert-success alert-dismissible'> <button type='button' class='close' data-dismiss='alert'>×</button>$success </div>"; } ?> <div class="card"> <div class="card-body login-card-body"> <p class="login-box-msg">Are you want to change Password?</p> <form action="" method="post"> <div class="input-group mb-3"> <input type="password" name="old_password" class="form-control" placeholder="Old Password" required=""> <div class="input-group-append"> <div class="input-group-text"> <span class="fa fa-lock"></span> </div> </div> </div> <div class="input-group mb-3"> <input type="password" name="new_password" class="form-control" placeholder="New Password" required=""> <div class="input-group-append"> <div class="input-group-text"> <span class="fa fa-lock"></span> </div> </div> </div> <div class="input-group mb-3"> <input type="password" name="con_password" class="form-control" placeholder="Confirm Password" required=""> <div class="input-group-append"> <div class="input-group-text"> <span class="fa fa-lock"></span> </div> </div> </div> <div class="row"> <div class="col-6"> <div class="icheck-primary"> <a href="home.php" class="text-center">Home Page</a> </div> </div> <div class="col-6"> <input type="submit" name="submit" class="btn btn-primary" value="Change Password"> </div> </div> </form> </div> </div> </div> <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script> </body> </html>
Output

Create Database Connection with PHPMyadmin
database.php
<?php class Database{ protected $localhost = "localhost"; protected $servername = "root"; protected $password = ""; protected $database = "scholarship"; public $con; public function connection(){ // Create connection $this->con = new mysqli($this->localhost, $this->servername, $this->password, $this->database); // Check connection if ($this->con->connect_error) { die("Connection failed: " . $this->con->connect_error); } return $this->con; } } ?>
Create a class file here the change function on this class File
students.php
<?php session_start(); include_once "database.php"; class Students { public $studentTable; public $dbObj; public $con; public function __construct() { $this->studentTable = "students"; $this->dbObj = new Database(); $this->con = $this->dbObj->connection(); } public function changePassword($id){ $new_password = md5($_POST['new_password']); $old_password = md5($_POST['old_password']); $con_password = md5($_POST['con_password']); if(!empty($new_password) && !empty($old_password) && !empty($con_password)){ if($new_password == $con_password){ $query = "SELECT * FROM $this->studentTable WHERE id = '$id'"; $result = $this->con->query($query); if ($result->num_rows > 0 ) { $row = $result->fetch_assoc(); $db_password = $row['password']; if($db_password == $old_password){ $update = "UPDATE $this->studentTable SET password = '$new_password' WHERE id = '$id'"; if ($this->con->query($update)==true) { return true; }else{ return false; } }else{ return false; } }else{ return false; } }else{ return false; } } } } $studentObj = new Students();
You can always support by sharing on social media or recommending my blog to your friends and colleagues.
If you have any suggestions / problems about this tutorial, please comment on the form below.😊
Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
https://espirittech.com/php-development/
This was a wonderful post being shared. The entire content in this blog is extremely helpful for me and gave me a clear idea of the concepts.
https://espirittech.com/php-development-services/