Rob*_*ert 1 delphi passwords delphi-7
Delphi v7
此代码旨在允许用户更改其密码.它似乎正确执行,但新密码未保存在密码数据字段中.我一定做错了什么,但我看不出来.
procedure TForm4.btnPswordClick(Sender: TObject);
var
I: integer;
begin
tblLogin.First;;
for I := 0 to tblLogin.RecordCount do
Begin
If tblLogin.FieldByName('Username').Value = Edit1.Text then
if tblLogin.FieldByName('Password').Value = Edit2.Text then
sign2.Visible := True; //Test in this case tells the application to make Label1
visible if the //username and password are correct
tblLogin.Next;
end;
I:= I+1; //ends search loop of records so program can move on
If sign2.Visible = False then
begin
MessageDlg('Error Username, or Password not correct',
mtConfirmation, [mbCancel], 0);
end
else
if edit3.Text <> edit4.Text then
begin
MessageDlg('Error New Password does not match',
mtConfirmation, [mbCancel], 0);
end
else
begin
tblLogin.Edit;
tblLogin.FieldByName('Password').Value := Edit3.Text;
tblLogin.Post;
//form1.Show;
//form4.Close;
end;
Run Code Online (Sandbox Code Playgroud)
我关于格式化代码的评论只是让我嗤之以鼻,但实际上我认为它确实会帮助你自己找到错误.正确缩进,你的第一个循环是这样的:
tblLogin.First;;
for I := 0 to tblLogin.RecordCount do
Begin
If tblLogin.FieldByName('Username').Value = Edit1.Text then
if tblLogin.FieldByName('Password').Value = Edit2.Text then
sign2.Visible := True; //Test in this case tells the application to make Label1 visible if the
//username and password are correct
tblLogin.Next;
end;
Run Code Online (Sandbox Code Playgroud)
下一行代码是这样的:
I:= I+1; //ends search loop of records so program can move on
Run Code Online (Sandbox Code Playgroud)
注释表明您希望该行导致循环终止.但那条线不在循环中.如果它在循环中,您的代码永远不会编译,因为您不允许在循环内修改循环控制变量.即使在循环之外,编译器也应警告您当前值I未定义.这是因为一旦循环终止,编译器不保证循环控制变量的最终值.将1添加到未定义的值仍然是未定义的值.
此外,您的循环遍历比数据库表包含的更多记录.如果一个for循环的上限没有从中减去1并且不是Highor 的结果Pred,那么你可能做错了.
由于您的I+1线路实际上并未终止循环,因此您最终会一直遍历到表格的末尾,因此当您呼叫时tblLogin.Edit,您将最终记录置于编辑模式,而不是具有匹配帐户详细信息的记录.
修复缩进还将显示您甚至没有包含该函数的所有代码.有一个begin没有匹配的end地方.
您可以通过UPDATE在数据库上使用单个语句来避免所有这些代码:
UPDATE tblLogin SET Password = ? WHERE Username = ? AND Password = ?
Run Code Online (Sandbox Code Playgroud)