Update Password using PHP

If you are looking for on how to Update Password u then you are at the right place. the right place. This simple tutorial creates using PHP/MySQL. The feature of this tutorial is to change the password from the database. It contains the Old Password from the database, New Password, and Re-Type New Password. Almost in web projects, changing password is important. Create Markup Form This simple HTML source code below contains three TextBox for the Old Password, New Password, and Re-Type Password. Where the user can update the password from the database. Review the source code below.
  1. <div style="width:30%;">
  2.    <form method="post" action="change-password.php">
  3.       <div>
  4.          <label style="color:blue;">Old Password</label>
  5.          <input type="password" name="old_pass" placeholder="Old Password . . . . .">
  6.       </div>
  7.       <div>
  8.          <label style="color:blue;">New Password</label>
  9.          <input type="password" name="new_pass" placeholder="New Password . . . . .">
  10.       </div>
  11.       <div>
  12.          <label style="color:blue;">Re-Type New Password</label>
  13.          <input type="password" name="re_pass" placeholder="Re-Type New Password . . . . .">
  14.       </div>
  15.       <button type="submit" name="re_password">Submit</button>
  16.    </form>
  17. </div>
Create Database Connection This would be your database connection.
  1. <?php
  2. $database_connection = mysql_connect("localhost", "root", "") or die();
  3. $select_database = mysql_select_db("change_password", $database_connection) or die();
  4. ?>
This is the PHP query where the UPDATE Statement is used to change the password from the database.
  1. <?php
  2.  
  3. if (isset($_POST['re_password']))
  4.         {
  5.         $old_pass = $_POST['old_pass'];
  6.         $new_pass = $_POST['new_pass'];
  7.         $re_pass = $_POST['re_pass'];
  8.         $password_query = mysql_query("select * from users where id='1'");
  9.         $password_row = mysql_fetch_array($password_query);
  10.         $database_password = $password_row['password'];
  11.         if ($database_password == $old_pass)
  12.                 {
  13.                 if ($new_pass == $re_pass)
  14.                         {
  15.                         $update_pwd = mysql_query("update users set password='$new_pass' where id='1'");
  16.                         echo "<script>alert('Update Sucessfully'); window.location='index.php'</script>";
  17.                         }
  18.                   else
  19.                         {
  20.                         echo "<script>alert('Your new and Retype Password is not match'); window.location='index.php'</script>";
  21.                         }
  22.                 }
  23.           else
  24.                 {
  25.                 echo "<script>alert('Your old password is wrong'); window.location='index.php'</script>";
  26.                 }
  27.         }
  28.  
  29. ?>
Database Name: change_password.sql Compile all the source code as you can see above to have this simple tutorial to change a password from the database or kindly click the "Download Code" button below for the full source code. Try and enjoy coding. Thank you.

Add new comment