سؤال

I'm sure I'm making a really stupid mistake somewhere here - so I'm creating this Change Password form and using a pretty shoddy attempt at OOP - so far, I don't have any of the 'changing password' code because I'm trying to sort out the erroring first.

So, my problem is this - when I submit the form, I get the error "Please fill out each field!" despite the fact all three fields are completed. Again, I'm sure I've made a really stupid mistake, but I could do with your help! Ty in advance! :)

Code at the top of my form page

if (empty($_POST) === false) {

  $password = trim($_POST['password']);
  $newpassword = trim($_POST['newpassword']);
  $confpassword = trim($_POST['confnewpassword']);
  $id = $_SESSION['id'];

  if (empty($password) === true || empty($newpassword) === true || empty($confnewpassword) === true) {
    $errors[] = 'Please fill out each field!';
  } else if ($users->current_password($password) === false) {
    $errors[] = 'Current password is incorrect!';
  } else if ($newpassword != $confpassword) {
    $errors[] = 'New password and Confirm password do not match!';
  } else if ($password == $newpassword){
    $errors[] = 'You cannot change your password to your existing password';
  }else {
      $errors[] = 'Password Changed!';
    }
} 

My current_password function

public function current_password($password) {

    $id = $_SESSION['id'];

    $query = $this->db->prepare("SELECT `password` FROM `users` WHERE `ID` = ?");
    $query->bindValue(1, $id);

    $enteredpassword == $password;

    try{

        $query->execute();
        $storedpass = $query->fetchColumn();

        if ($storedpass === sha1($enteredpassword)) {
            return true;
        } else {
            return false;
        }

    } catch (PDOException $e){
        die($e->getMessage());
    }


}

My HTML Form

<form class="form-horizontal" role="form" method="POST">
  <div class="form-group">
    <label class="col-sm-4 control-label">EIN / Username</label>
    <div class="col-sm-8">
      <input class="form-control" id="disabledInput" type="text" placeholder="<?php echo $ein; ?>" disabled>
    </div>
  </div>
  <div class="form-group">
    <label for="inputPassword" class="col-sm-4 control-label">Enter Existing Password</label>
    <div class="col-sm-8">
      <input type="password" class="form-control" name="password" placeholder="Enter Existing Password">
    </div>
  </div>
  <div class="form-group">
    <label for="inputPassword" class="col-sm-4 control-label">Enter New Password</label>
    <div class="col-sm-8">
      <input type="password" class="form-control" name="newpassword" placeholder="Enter New Password">
    </div>
  </div>
  <div class="form-group">
    <label for="inputPassword" class="col-sm-4 control-label">Confirm New Password</label>
    <div class="col-sm-8">
      <input type="password" class="form-control" name="confnewpassword" placeholder="Confirm New Password">
    </div>
  </div>
  <button type="submit" class="btn btn-primary btn-block">Change Password</button>
</form>
هل كانت مفيدة؟

المحلول

Your confime password varibale name is $confpassword

$confpassword = trim($_POST['confnewpassword']);

your are using $confnewpassword in

if (empty($password) === true || empty($newpassword) === true || empty($confnewpassword) === true)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top