Building a Server Application for an Internet Cafe - Code Explained
(Page 3 of 4 )
Let's step through the code. The first event handling procedure is fired when the staff member clicks on the logon button. Keep in mind that the staff member is required to enter a username and password to kick start the authentication process. The first part of the procedure checks to see if the staff member has entered her username and password:
if (uname.text = '') OR (upass.Text='') then begin
MessageDlg('Please make sure that you fill in both the username and password fields.', mtInformation,
[mbOk], 0);
exit;
end;
If the staff member has not entered their login credentials and presses the login button, an error message is displayed informing the user to enter their details, as the code shows. Otherwise the procedure continues the authentication process by checking to see if the login credentials that the staff member entered exist in the database. The code uses the TADOQuery component, referred to as "q" to connect to the "users" table and then to run a SQL query to check if the credentials entered by the staff member match those in the database:
q.close;
q.SQL.Text:='SELECT * from users WHERE username=:uname AND password=:upass';
q.Parameters.ParamByName('uname').Value:=uname.Text;
q.Parameters.ParamByName('upass').Value:=upass.Text;
Next the query is "opened" to see if any results have been returned:
q.Open;
The number of records retrieved will be stored in the "Recordcount" property of the query component. If the value of recordcount is greater than zero, then it means that the credentials that the staff member entered were found in the database, in which case the user is authenticated:
if q.RecordCount > 0 then begin
Next: Code explanation continued >>
More Delphi-Kylix Articles
More By David Web