Let's take a look how to change password using PHP and MYSQL
Create table using below sql commands:
CREATE TABLE IF NOT EXISTS `register` (
`UserId` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(100) NOT NULL,
`Email` varchar(100) NOT NULL,
`Username` varchar(100) NOT NULL,
`Password` varchar(100) NOT NULL,
`activation` varchar(100) NOT NULL,
PRIMARY KEY (`UserId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Functionality files:
1.conn.php
2.change_password.php
Conn.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db_name = "email"; //replace with your database name
//database connection
$link = mysql_connect($host, $user, $pass);
$db=mysql_select_db($db_name);
?>
change_password.php
<?php
//Database Config File
require_once "conn.php";
if(isset($_POST['Submit'])){
$email=$_POST['email'];
$Password=$_POST['password'];
$newpassword=$_POST['newpassword'];
//Query for check user exits in database
$Password_update=mysql_query("SELECT Password FROM register WHERE Email='$email'");
while($row=mysql_fetch_array($Password_update)){
//fetch stored user password
$rowpassword=$row['Password'];
}
if($rowpassword != $Password){
//Check current password and entered password is correct
echo "You have entered wrong password.Enter correct current password";
}
else
{
$update_password=mysql_query("Update register SET Password='$newpassword' WHERE Email='$email'" ); //Update query for Password
if($update_password){
echo "Password changed successfully";//Success message
}
else
{
echo "Error";
}
}
}
?>
<form action="#" id="register" method="post">
<table border="0">
<tbody>
<tr>
<td><label for="email">Email:</label>
</td>
<td><input id="email" maxlength="45" name="email" type="text" /></td>
</tr>
<tr>
<td><label for="Current Pasword">Current Password:</label>
</td>
<td><input id="currentpassword" maxlength="45" name="password" type="password" />
</td>
</tr>
<tr>
<td><label for="password">New Password:</label></td>
<td><input id="newpassword" maxlength="45" name="newpassword" type="password" /></td>
</tr>
<tr>
<td><label for="password">Confirm Password:</label></td>
<td><input id="confirmpassword" maxlength="45" name="confirmpassword" type="password" /></td>
</tr>
<tr>
<td align="right"><input name="Submit" type="submit" value="Submit" /></td>
</tr>
</tbody>
</table>
</form>
Create table using below sql commands:
CREATE TABLE IF NOT EXISTS `register` (
`UserId` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(100) NOT NULL,
`Email` varchar(100) NOT NULL,
`Username` varchar(100) NOT NULL,
`Password` varchar(100) NOT NULL,
`activation` varchar(100) NOT NULL,
PRIMARY KEY (`UserId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Functionality files:
1.conn.php
2.change_password.php
Conn.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db_name = "email"; //replace with your database name
//database connection
$link = mysql_connect($host, $user, $pass);
$db=mysql_select_db($db_name);
?>
change_password.php
<?php
//Database Config File
require_once "conn.php";
if(isset($_POST['Submit'])){
$email=$_POST['email'];
$Password=$_POST['password'];
$newpassword=$_POST['newpassword'];
//Query for check user exits in database
$Password_update=mysql_query("SELECT Password FROM register WHERE Email='$email'");
while($row=mysql_fetch_array($Password_update)){
//fetch stored user password
$rowpassword=$row['Password'];
}
if($rowpassword != $Password){
//Check current password and entered password is correct
echo "You have entered wrong password.Enter correct current password";
}
else
{
$update_password=mysql_query("Update register SET Password='$newpassword' WHERE Email='$email'" ); //Update query for Password
if($update_password){
echo "Password changed successfully";//Success message
}
else
{
echo "Error";
}
}
}
?>
<form action="#" id="register" method="post">
<table border="0">
<tbody>
<tr>
<td><label for="email">Email:</label>
</td>
<td><input id="email" maxlength="45" name="email" type="text" /></td>
</tr>
<tr>
<td><label for="Current Pasword">Current Password:</label>
</td>
<td><input id="currentpassword" maxlength="45" name="password" type="password" />
</td>
</tr>
<tr>
<td><label for="password">New Password:</label></td>
<td><input id="newpassword" maxlength="45" name="newpassword" type="password" /></td>
</tr>
<tr>
<td><label for="password">Confirm Password:</label></td>
<td><input id="confirmpassword" maxlength="45" name="confirmpassword" type="password" /></td>
</tr>
<tr>
<td align="right"><input name="Submit" type="submit" value="Submit" /></td>
</tr>
</tbody>
</table>
</form>
No comments:
Post a Comment