change password in php mysql code
This concept based on Client side user change the old password to new password. just simple way to change password in database using update query to validate the old password and change the new password. post the form value to store php variable and fetch the old password use if and else statement check the validate on old password if password match query will update the new password in database.
This concept based on Client side user change the old password to new password. just simple way to change password in database using update query to validate the old password and change the new password. post the form value to store php variable and fetch the old password use if and else statement check the validate on old password if password match query will update the new password in database.
Database
create below fields in database table insert some sample value.
CREATE TABLE `users` (
`username` VARCHAR( 25 ) NOT NULL ,
`email` VARCHAR( 25 ) NOT NULL ,
`password` VARCHAR( 25 ) NOT NULL
)
INSERT INTO users VALUES ('user', 'you@example.com', 'abc123');
create below fields in database table insert some sample value.
CREATE TABLE `users` ( `username` VARCHAR( 25 ) NOT NULL , `email` VARCHAR( 25 ) NOT NULL , `password` VARCHAR( 25 ) NOT NULL ) INSERT INTO users VALUES ('user', 'you@example.com', 'abc123');
PHP
fetch the email address in database Post old password value to store $passwordvalue=$_POST['cur_pwd']; using this if else statement password is correct $login1="password changed"; echo the form else display $login1="password not match plz try again";
<?php
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost
$db_user = "root"; // change to your database password
$db_password = ""; // change to your database password
$database = "user"; // provide your database name
//$db_table = "table"; // Your Table Name where you want to Store Your Image.
# STOP HERE
####################################################################
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
if(isset($_POST['submit']))
{
function check_input($value)
{
// Stripslashes
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
// Quote if not a number
if (!is_numeric($value))
{
$value = "" . mysql_real_escape_string($value) . "";
}
return $value;
}
$passwordvalue=$_POST['cur_pwd'];
$password=$_POST['password'];
$confirm_pwd=$_POST['confirm_pwd'];
$cur_pass=md5($passwordvalue);
$new_pass=md5($password);
$conf_pass=md5( $confirm_pwd);
$username=$_SESSION['userName'];
$select=mysql_query("select * from ABC where userName='$username'");
$fetch=mysql_fetch_array($select);
$data_pwd=$fetch['repassWord'];
$email=$fetch['user_email'];
if($new_pass==$conf_pass && $data_pwd==$cur_pass)
{
$insert=mysql_query("UPDATE ABC SET passWord='$conf_pass',repassWord='$conf_pass' WHERE userName='$username'");
$login1="password changed";
}
else
{
$login1="password not match plz try again";
}
}
?>
fetch the email address in database Post old password value to store $passwordvalue=$_POST['cur_pwd']; using this if else statement password is correct $login1="password changed"; echo the form else display $login1="password not match plz try again";
<?php
$hostname = "localhost"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost
$db_user = "root"; // change to your database password
$db_password = ""; // change to your database password
$database = "user"; // provide your database name
//$db_table = "table"; // Your Table Name where you want to Store Your Image.
# STOP HERE
####################################################################
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
if(isset($_POST['submit']))
{
function check_input($value)
{
// Stripslashes
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
// Quote if not a number
if (!is_numeric($value))
{
$value = "" . mysql_real_escape_string($value) . "";
}
return $value;
}
$passwordvalue=$_POST['cur_pwd'];
$password=$_POST['password'];
$confirm_pwd=$_POST['confirm_pwd'];
$cur_pass=md5($passwordvalue);
$new_pass=md5($password);
$conf_pass=md5( $confirm_pwd);
$username=$_SESSION['userName'];
$select=mysql_query("select * from ABC where userName='$username'");
$fetch=mysql_fetch_array($select);
$data_pwd=$fetch['repassWord'];
$email=$fetch['user_email'];
if($new_pass==$conf_pass && $data_pwd==$cur_pass)
{
$insert=mysql_query("UPDATE ABC SET passWord='$conf_pass',repassWord='$conf_pass' WHERE userName='$username'");
$login1="password changed";
}
else
{
$login1="password not match plz try again";
}
}
?>
HTML
Enter the form value post on database
<form method="post" name="change">
<table border="0" cellpadding="0.1" style="margin-left:100px;">
<tr>
<td ><h2 class="style8">Change Password</h2></td>
<td></td>
</tr>
<tr>
<td> </td>
<td><?php echo $login1 ; ?></td>
</tr>
<tr>
<td class="style10">old password<span class="style11">*</span>:</td>
<td><input type="password" name="cur_pwd" id="cur_pwd" class="ser" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td class="style10">New password<span class="style11">*</span>:</td>
<td><input type="password" name="password" id="password" class="ser" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td class="style10">Confirm password<span class="style11">*</span>:</td>
<td><input type="password" name="confirm_pwd" id="confirm_pwd" class="ser" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td align="center"><input name="submit" id="submit" type="submit" value="Save" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
Enter the form value post on database
<form method="post" name="change">
<table border="0" cellpadding="0.1" style="margin-left:100px;">
<tr>
<td ><h2 class="style8">Change Password</h2></td>
<td></td>
</tr>
<tr>
<td> </td>
<td><?php echo $login1 ; ?></td>
</tr>
<tr>
<td class="style10">old password<span class="style11">*</span>:</td>
<td><input type="password" name="cur_pwd" id="cur_pwd" class="ser" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td class="style10">New password<span class="style11">*</span>:</td>
<td><input type="password" name="password" id="password" class="ser" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td class="style10">Confirm password<span class="style11">*</span>:</td>
<td><input type="password" name="confirm_pwd" id="confirm_pwd" class="ser" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td align="center"><input name="submit" id="submit" type="submit" value="Save" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>