How to create Reset Password in PHP MySQL

0 16,454

Hi Friends, in this post we are going to learn how to create reset password in PHP MySQL. Reset password is very important for any web application.  They maybe exist in a content management system, and an inventory management system OR accounting application. if you just started web development, you are certainly going to experience lotsReset password form creation work in your later career.

How to create Reset Password in PHP MySQL

1-Creating Database

  • Open PHPMyAdmin in your Browser
  • Click on Database Tab Display on Topside
  • Give the Database name “password”.
  • After Creating Database Open it.
  • Click on SQL Tab on Top area
  • Copy the Below Source Code and paste it.
  • Then Click on Go.
-- phpMyAdmin SQL Dump
-- version 4.8.2
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Oct 27, 2018 at 02:55 PM
-- Server version: 10.1.34-MariaDB
-- PHP Version: 5.6.37

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `password`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `name` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `name`, `password`) VALUES
(1, 'admin', '123456');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

OR Import DB File

After Downloading the source code extract it in your root folder.

  • Open PHPMyAdmin in your Browser
  • Click on Database Tab Display on Topside
  • Give the Database name “password”.
  • After Creating Database Open it.
  • Click on Import Tab on Top area
  • You can Find Db file in  Downloaded source code Select it.
  • Then Click on Go.

2- Creating Database Connection

After import Database File then the next step is creating database connection. here the source code of database connection. this code available in reset.php file.

$conn_db = mysql_connect("localhost","root","") or die();
$sel_db = mysql_select_db("password",$conn_db) or die();

3- Creating Reset Password Form

in this step we are going to create reset password form using html. you copy the below code and paste in your project folder and save it as index.php.

<!DOCTYPE html>
<html>
	<head>
		<title>How to create Reset Password in PHP MySQL</title>
		<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
		<link rel="stylesheet" type="text/css" href="css/style.css"/>
	</head>

<body>
	<!-- Form Open -->
	<fieldset>
		<legend>Reset Password</legend>
		<!-- Include PHP Script -->
		<?php include 'reset.php';?>
		
		<form method="post">
			<dl>
				<dt>
					Old Password
				</dt>
					<dd>
						<input type="password" name="old_pass" placeholder="Enter Old Password..." value="" required />
					</dd>
			</dl>
			
			<dl>
				<dt>
					New Password
				</dt>
					<dd>
						<input type="password" name="new_pass" placeholder="Enter New Password..." value=""  required />
					</dd>
			</dl>
			
			<dl>
				<dt>
					Retype New Password
				</dt>
					<dd>
						<input type="password" name="re_pass" placeholder="Retype New Password..." value="" required />
					</dd>
			</dl>
			
			<p align="center">
				<input type="submit" class="btn" value="Reset Password" name="re_password" />
			</p>
		</form>
	</fieldset>
	<!-- Form Close -->
	
</body>
</html>

4- Styling The HTML Form

in this step we are going to make some style for our form using css. copy the below code and save it as style.css in your project folder.

fieldset {
	width:500px;
	border:2px solid green;
	margin:0 auto;
	border-radius:5px;
}

legend {
	color: black;
	font-size: 25px;
}

dl {
	float: right;
	width: 390px;
}

dt {
	width: 180px;
	padding-bottom:5px;
	margin-bottom:5px;
	color: black;
	font-size: 19px;
}

dd {
	width:200px;
	float:left;
}

dd input {
	width: 200px;
	border: 1px solid #DDD;
	font-size: 15px;
	text-indent: 5px;
	height: 28px;
}
dd input:hover {
	width: 200px;
	border: 1px solid green;
	font-size: 15px;
	text-indent: 5px;
	height: 28px;
}

.btn {
	color: #fff;
	background-color: green;
	height: 38px;
	border: 2px solid #CCC;
	border-radius: 10px;
	float: right;
}

5- Reset Password Script in PHP

in this step we are going to create php code to reset the password into mySQL database. here the source code copy below code and save it as reset.php

<?php 
	$conn_db = mysql_connect("localhost","root","") or die();
	$sel_db = mysql_select_db("password",$conn_db) or die();
	if(isset($_POST['re_password']))
	{
	$old_pass=$_POST['old_pass'];
	$new_pass=$_POST['new_pass'];
	$re_pass=$_POST['re_pass'];
	$chg_pwd=mysql_query("select * from users where id='1'");
	$chg_pwd1=mysql_fetch_array($chg_pwd);
	$data_pwd=$chg_pwd1['password'];
	if($data_pwd==$old_pass){
	if($new_pass==$re_pass){
		$update_pwd=mysql_query("update users set password='$new_pass' where id='1'");
		echo "<script>alert('Update Sucessfully'); window.location='index.php'</script>";
	}
	else{
		echo "<script>alert('Your new and Retype Password is not match'); window.location='index.php'</script>";
	}
	}
	else
	{
	echo "<script>alert('Your old password is wrong'); window.location='index.php'</script>";
	}}
?>

If you facing any type of problem with this source code then you can Download the Complete source code in zip Formate by clicking the below button Download Now otherwise you can send Comment.

How to create Reset Password in PHP MySQL

Download Source Code

Leave A Reply

Your email address will not be published.