PHP Change Password script using Mysqli

PHP Change Password script using Mysqli

The Change password feature option(in a setting) in a web application is to let the all user change or admin change their old password(at a security) at some periodic interval In this Post Learn to PHP Change Password script using Mysqli

PHP Change Password Script using mysqli. After the change password successful your form submits, the PHP server side code will access Mysqli database to get or fetch current password from the Database retrived. If this database value is both value matched with the your forms current change password value, then the password will be successfully changed.

Also Read :  Send email using nodejs and express in Nodemailer steps

Core Logic

  • First step, you have created simple HTML form with respective all the text-filed(old password, new password, Retype password).
  • Check the basic validation whether the new password and Retype password is same or not and all empty fields validation.
  • After the check javascript validation,and then check serverside you have to check old password (true or false) valid or not. The password is the same or match then allow skip to next level.
  • Simple old password type and new password type is same(Both are same) don’t allow the any option change the password.

PHP Change Password Script

	$result =$db->query("SELECT *from users WHERE id='" . $_SESSION["userId"] . "'");
	if($pass_encrypt == $row["password"]) {
	$str=$db->query("UPDATE users set password='" . $passnew_encrypt . "' WHERE id='" . $_SESSION["userId"] . "'");
	$message = "You have successfully changed your password.";
	} else $message = "Current Password is not correct";


HTML Code for Change Password Form

<form name="frmChange" action="" method="POST" onSubmit="return validatePassword()">
	<div class="form-group">
		<label>Current Password: <span id="defaultcurrentPass"  class="required"></span></label>
	<input type="password" name="defaultcurrentPass" class="form-control"/>
	<div class="form-group">
		<label>New Password: <span id="changenewPass" class="required"></span></label>
		<input type="password" name="changenewPass" class="form-control"/>
	<div class="form-group">
		<label>Retype New Password: <span id="RetypePassword" class="required"></span></label>
	<input type="password" name="RetypePassword" class="form-control"/>
				<input type="hidden" value="<?php echo $cms_desc['ofid']; ?>"name="hofid"/>
				<input type="submit" name="updatepass"  class="btn btn-primary" value="Update Password"/>

Also Read :  PHP codeigniter form validation Example

function validatePassword() {
var defaultcurrentPass,changenewPass,RetypePassword,output = true;

defaultcurrentPass = document.frmChange.defaultcurrentPass;
changenewPass = document.frmChange.changenewPass;
RetypePassword = document.frmChange.RetypePassword;

if(!defaultcurrentPass.value) {
	document.getElementById("defaultcurrentPass").innerHTML = "required";
	output = false;
else if(!changenewPass.value) {
	document.getElementById("changenewPass").innerHTML = "required";
	output = false;
else if(!RetypePassword.value) {
	document.getElementById("RetypePassword").innerHTML = "required";
	output = false;
if(changenewPass.value != RetypePassword.value) {
	document.getElementById("RetypePassword").innerHTML = "Both Password are not same";
	output = false;
return output;

CSS Code For Display Message and Validation

.required {
color: #FF0000;

.message {
color: #FF0000;
text-align: center;
width: 100%;

View Demo

Leave a Reply

Your email address will not be published. Required fields are marked *