我有一个表单,它通过编辑框和组合框从用户那里获取数据,然后这些信息必须上传到一个数据库表中,所以我必须进行验证。我做了验证,它一直说“不是一个有效的整数”,但它甚至不应该上传任何东西到数据库表中,因为所有的条件都不满足,因为我做了空值检查。我做了错误的验证吗?
unit Unit6;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Spin, ExtCtrls,unit7,unit5,unit4, DB, ADODB, Grids, DBGrids,
pngimage;
type
Tfrmupload = class(TForm)
Panel1: TPanel;
edtname: TEdit;
edtsurn: TEdit;
edtidn: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label5: TLabel;
Label6: TLabel;
Panel2: TPanel;
btnupload: TButton;
edtmail: TEdit;
cbprov: TComboBox;
Image1: TImage;
cbdiv: TComboBox;
Label4: TLabel;
procedure btnuploadClick(Sender: TObject);
procedure Label7Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmupload: Tfrmupload;
implementation
{$R *.dfm}
procedure Tfrmupload.btnuploadClick(Sender: TObject);
var
vname,vsurname,today,c,vidn,vprov,vmail,vdiv:string;
vage,year,i,age:integer;
bnotnull,bIDL,bmail,bage,byoung,valid,idrepeated:boolean;
dt:tdatetime;
begin
vname:=edtname.Text;
vsurname:=edtsurn.Text;
vidn:=edtidn.Text;
vdiv:=cbdiv.Text;
vprov:=cbprov.Text;
vmail:=edtmail.Text;
//booleans
bnotnull:=false;
bIdL:=false;
bmail:=false;
byoung:=false;
idrepeated:=false;
valid:=false;
//check if id entered is not in table already
if unit5.frmadmin.ADODetails.Locate('ID number',vidn,[]) then
begin
idrepeated:=true;
showmessage('ID Number already exists');
end;
//null check
if (vname<>'')and(vsurname<>'')and(vidn<>'')and(vprov<>'')and(vmail<>'')and(vdiv<>'') then
begin
bnotnull:=true;
end
else
begin
Showmessage('Complete All Fields!');
end;
//get current date
dt:=now;
year:=strtoint(formatdatetime('yyyy',dt));
c:=copy(vidn,1,2);
//calculate age from id number
if (strtoint(c)>=0) and (strtoint(c)<=22) then
begin
age:=year-(2000+strtoint(c));
end
else
begin
age:=year-(1900+strtoint(c));
end;
//ID length validation
if (length(vidn)=13) then
begin
bidl:=true;
end;
//check if contestant is not too young
if age<6 then
begin
showmessage('Contestant too young, cannot compete');
byoung:=true;
end;
//check if email is correct format
for i := 1 to length(vmail)do
begin
if vmail[i]='@' then
begin
bmail:=true;
end;
end;
//error message for email check
if bmail=false then
begin
showmessage('Incorrect Email Format');
end;
if bidl=false then
begin
showmessage('ID Number must be 13 characters');
end;
//checks if all the conditions are met and if so we can then upload to database
if (bnotnull=true) and (bidl=true) and (byoung=false)and (bmail=true)and (idrepeated=false) then
begin
valid:=true;
end;
if valid=true then
begin
unit5.frmadmin.ADODetails.Append;
unit5.frmadmin.ADODetails['Name(s)']:=vname;
unit5.frmadmin.ADODetails['Surname']:=vsurname;
unit5.frmadmin.ADODetails['ID Number']:=vidn;
unit5.frmadmin.ADODetails['Age']:=age;
unit5.frmadmin.ADODetails['Province']:=vprov;
unit5.frmadmin.ADODetails['Email']:=vmail;
unit5.frmadmin.ADODetails.Post;
unit4.frmcontest.ADOLead.Insert;
unit4.frmcontest.ADOLead['ID Number']:=unit4.frmcontest.DBComboID.Text;;
unit4.frmcontest.ADOLead['Name(s)']:=unit4.frmcontest.DBCombonme.Text;
unit4.frmcontest.ADOLead['Division']:=vdiv;
unit4.frmcontest.ADOLead.Post;
showmessage('Details Uploaded');
frmupload.Hide;
end;
//clears all inputs
edtname.Clear;
edtsurn.Clear;
edtidn.Clear;
cbdiv.Text:='';
cbprov.Text:='';
edtmail.Clear;
end;
procedure Tfrmupload.Label7Click(Sender: TObject);
begin
frmupload.Hide;
end;
end.
2条答案
按热度按时间uujelgoq1#
错误是很明显的。你有strtoint(又名STRING到INT),但是你给予了它一个格式化的日期时间。
我不明白为什么要用这样的方式来表示年份,因为你可以直接把2022的值赋给整数,但是如果你坚持这样做,你可以先格式化dt,然后使用datetimetostr,然后使用strtoint。另一个可以减少代码和工作量并使一切变得更容易的东西是DB-aware组件。它们的作用是,一旦你把它们连接到数据库和你正在使用的表和列,它们会自动进行更新或插入。你所需要做的就是在添加新行时使用TMSQuery.Insert,然后使用TMSQuery.Post,在更改值时使用TMSQuery.Edit和TMSQuery.Post。
js5cn81o2#
我认为我们需要看到一个样本数据库记录,它被保存到带有错误条件的数据库中。此外,您可以通过将Trim()函数应用到编辑框的输入中来采取预防措施
您还可以检查变量vidn的长度