Home > Enterprise >  Nested if statement not working correctly
Nested if statement not working correctly

Time:10-10

So I am making a project that allows a user to change their password in a database, one of the latter stages of the code which is that they must re-enter the correct password again in order for it to change is not working here it is:

if tblusers.locate('StudentID', sStudentID,[]) then
begin
  if soldpass = tblusers['Password'] then
  else
  begin
    showmessage('Incorrect Current Password');
    exit;
  end;
  tblusers.edit;
  tblusers['Password'] := snewpass;
  if snewpass <> sreenter then
  begin
    showmessage('Re-Enter your Password correctly');
    edtreenter.clear;
    exit;
  end
  else
    tblusers.post;
  showmessage('Password Successfully Changed');
end;

The problem is that last if statement is not working properly because they can re enter a totaly different password and it will still change it

CodePudding user response:

you have to use small and capital letters. your text message is difficult to understand, don't use same words for different messages type.

var
  ...
  s_Msg : string;
begin
  if sOldPass <> tblUsers['Password'] then
    s_Msg := 'wrong password' // 'login failed', 'incorrect password' ...
  else
  begin
    ShowMessage('login successful'); // 'password is correct', 'login done' ...
    tblUsers.edit;

    if sNewPass <> sReenter then
    begin
      edtreEnter.clear;
      s_Msg := 're-enter password is incorrect'; // 'new password is wrong', 'please confirm your password' ...
    end
    else
    begin
      tblUsers['Password'] := sNewPass;
      tblUsers.post;
      s_Msg := 'password successfully changed';
    end;
  end;
  ShowMessage(s_Msg);
end;

CodePudding user response:

So I have fixed the issue after consulting with my dad and him helping me better my code This the the new code which works:

begin
  if soldpass = tblusers['Password'] then
  begin
    showmessage('Correct Current Password');
    tblusers.edit;
    if snewpass = sreenter then
    begin
      tblusers['Password'] := snewpass;
      tblusers.post;
      showmessage('Password Successfully Changed');
    end
    else
    begin
      showmessage('Re-Enter your Password correctly');
      edtreenter.clear;
      exit;
    end;
  end
  else
  begin
    showmessage('Incorrect Current Password');
    exit;
  end;
end
  • Related