最后的例子:
最后,我准备以一个例子来结束这个教程。
环境:
这是一个非常简单的WEB应用程序。如果你要运行这个程序,也许你得化些时间来配置自己的系统。在这个例子里,CGI程序由Perl5来编写,后台数据库使用sybase
system 10,Sybperl用于建立脚本和数据库之间的通信,用其他的CGI后台数据库接口程序也可以。
CGI程序:
<head>
<title>会议登记程序</title>
</head>
<body>
<h2>会议登记系统</h2>
please fill out the following form. A registration packet will be sent to you immediatel
you.Note that all <b>bolad-faced</b>fields muse be complected.
<hr>
<form method="post" action="cgi-bin/resgister.pl">
<b>first name:</b><input type="text" name="c_fname"
size=30>
<p>
<b>Last name:</b><input type="text" name="c_lname"
size=30>
<p>
<b>address 1:</b><input type="text" name="c_add1"
size=30>
<p>
<b>address 2:</b><input type="text" name="c_add2"
size=30>
<p>
<b>city:</b><input type="text" name="c_city"
size=30>
<p>
<b>State:</b><input type="text" name="c_state"
size=2>
<p>
<b>Zip:</b><input type="text" name="c_zip" size=15>
<p>
<b>phone:</b><input type="text" name="c_phone"
size=15>
<p>
<b>Fax:</b><input type="text" name="c_fax" size=15>
<p>
<b>Email:</b><input type="text" name="c_email"
size=15>
<p>
Packge:
<ul>
<li><input type="radio" name="c_packge"
valuge="a">Package A
<li><input type="radio" name="c_packge"
valuge="b">Package B
<li><input type="radio" name="c_packge"
valuge="c">Package C
</ul>
How did you hear about the conference:
<ul>
<li><input type="radio" name="c_ad"
value="a">Direct Mail
<li><input type="radio" name="c_ad"
value="b">Associate
<li><input type="radio" name="c_ad"
value="c">Journal
<li><input type="radio" name="c_ad"
value="d">Other
</ul>
<p>
<input type="reset" value="clear"><input
type="submit" value="Register"><p>
</form>
<hr>
</body>
Perl脚本:
#!/usr/local/bin/perl
require"cgi-lib.pl";
&ReadParse(* input);
$mc_fname=$input{'c_fname'};
$mc_lname=$input{'c_lname'};
$mc_add1=$input{'c_add1'};
$mc_add2=$input{'c_add2'};
$mc_city=$input{'c_city'};
$mc_state=$input{'c_state'};
$mc_zip=$input{'c_zip'};
$mc_phone=$input{'c_phone'};
$mc_fax=$input{'c_fax'};
$mc_email=$input{'c_email'};
$mc_package=$input{'c_package'};
$mc_ad=$input{'c_ad'};
print &PrintHeader;
if (($mc_fname eq"")||(mc_lname eq
"")||(mc_add1 eq "")||($mc_add2 eq "")||(
mc_city eq "")||(mc_state eq "")||(mc_zip eq "")){
printf ("<br>");
printf(",strong>Your registration cannot be processed as is.Pleae make sure all
the requied fields are filled properly.</strong>");
print"<p>";
print"Pleae use the <i>Back</i>button to complete the form.\n";
}
else{# data is OK
use Sybase::DBlib;
$dbh=Sybase::DBlib->dblogin('conf_user','123','DB-SERVER','REG');
$dbh->dbuse('conference');
$sqlcmd="insert into
reg(c_fname,c_lanme,c_add1,c_add2,c_city,c_state,c_zip,
c_phone,c_fax,c_email,c_package,c_ad)";
$sqlcmd=$sqlcmd."values("
$sqlcmd=$sqlcmd.'\"".$mc_fname."\",";
$sqlcmd=$sqlcmd.'\"".$mc_flame."\",";
$sqlcmd=$sqlcmd.'\"".$mc_add1."\",";
$sqlcmd=$sqlcmd.'\"".$mc_add2."\",";
$sqlcmd=$sqlcmd.'\"".$mc_city."\",";
$sqlcmd=$sqlcmd.'\"".$mc_state."\",";
$sqlcmd=$sqlcmd.'\"".$mc_zip."\",";
$sqlcmd=$sqlcmd.'\"".$mc_phone."\",";
$sqlcmd=$sqlcmd.'\"".$mc_fax."\",";
$sqlcmd=$sqlcmd.'\"".$mc_email."\",";
$sqlcmd=$sqlcmd.'\"".$mc_package."\",";
$sqlcmd=$sqlcmd.'\"".$mc_ad."\",";
$sqlcmd=$sqlcmd.'\"".$mc_fax."\",";
$dbh->dbcmd($sqlcmd."\n");
$dbh->dbsqlexec;
print"<strong>Thank you.</strong>Your registration has been submitted.You
should receive you packet shourtly in the mail.";
}