嵌套的IF语句无法正常工作

osh3o9ms  于 2022-10-16  发布在  其他
关注(0)|答案(2)|浏览(229)

所以我正在做一个项目,允许用户在数据库中更改他们的密码,这是代码的后一个阶段,他们必须再次输入正确的密码才能更改,它不起作用如下:

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;

问题是最后一条if语句不能正常工作,因为他们可以重新输入一个完全不同的密码,但它仍然会更改它

mctunoxg

mctunoxg1#

您必须使用小写和大写字母。您的短信很难理解,请不要为不同的短信类型使用相同的词语。

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;
mepcadol

mepcadol2#

因此,在咨询了我父亲和他帮助我改进我的代码后,我修复了这个问题。这是一个有效的新代码:

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

相关问题