Notice: Undefined index: username

<body class="bg-dark">
  <div class="container">
    <div class="card card-login mx-auto mt-5">
      <div class="card-header">Change Password</div>
      <div class="card-body">
        <form method="POST" action="change-password.php">
          <div class="form-group">
            <label for="username">Username</label>
            <input class="form-control" id="username" type="text" name="username" aria-describedby="nameHelp" placeholder="Enter username" required>
          </div>
          <div class="form-group">
            <label for="New Password">New Password</label>
            <input class="form-control" id="newpass" type="text" name="newpass" placeholder="Enter new password" required>
          </div>
            <div class="form-group">
            <label for="Confirm Password">Confirm Password</label>
            <input class="form-control" id="confpass" type="text" name="confpass" placeholder="Enter confirm password" required>
          </div>
          <div class="form-group">
            <div class="form-check">
              <label class="form-check-label">
                <input class="form-check-input" type="checkbox"> Remember Password</label>
            </div>
          </div>
          <div class="form-group">
              <button type="submit" name="submit" class="btn">Submit</button> <br>
          </div>
        </form>
        <div class="text-center">
          <a class="d-block small mt-3" href="login.php">Login</a>
        </div>
      </div>
    </div>
  </div>


  <?php 

$con=mysqli_connect("localhost","root","","goods_management_system") or die(mysqli_error());
$db=mysqli_select_db($con, 'goods_management_system' ) or die(mysqli_error());
if(isset($_POST['submit'])){
  $username=$_POST['username'];
  $newpass = $_POST['newpass'];
  $confpass = $_POST['confpass'];

  $query = "SELECT username FROM user_details WHERE username='$username'";
  $run = mysqli_query($con, $query);
  $row = mysqli_fetch_row($run);

  if($row['username']==$username)
  {
    if($newpass == $confpass)
    {
      $update_query = "UPDATE user_details SET password='$newpass' WHERE username='$username'";

      if($update_query)
      {
        echo "<script>alert('Password change successfully')</script>";
      }
      else
      {
        "<script>alert('Password change failed')</script>";
      }
    }
    else 
    {
      echo "<script>alert('password doesnt match')</script>";
    }
  }
}
?>

Hi subashan05 welcome to the forum.

Actually, I believe the problem is the code uses mysqli_fetch_row

returns it as an enumerated array, where each column is stored in an array offset starting from 0 (zero).

In other words, the code should be changed to either use $row[0] etc. or to use a function that returns associative indexes.
`

2 Likes

On another note, you should always be putting all form submissions at the very beginning of your file. This helps reduce simple errors like headers already sent errors and makes your code consistent and easy to read if you happen to use the same PHP codes on different files.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.