我是一名学生,也是Perl的新手,可以帮助解决为什么这不能正常执行。即使输入的标准正确,它也会显示所有错误消息。我希望它能够显示需要时应用的错误,或者如果所有字段都符合正确的标准则显示感谢信息。
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use CGI qw(:standard);
$rn = param("Reservation Name");
$rc = param("Reservation Create Date");
$rr = param("Reservation Room Type");
$rnd = param("Reservation Number of Days");
$rs = param("Reservation Start Date");
$re = param("Reservation Room Cost");
$rp = param("Reservation Payment Method");
$rd = param("Reservation Discount Code");
$e1 = 0;
$e2 = 0;
$e3 = 0;
$e4 = 0;
$e5 = 0;
$e6 = 0;
$e7 = 0;
$e8 = 0;
$errors = 0;
if ( $rn eq "")
{
$e1 = 1;
$errors =1;
}
if ( $rc eq "")
{
$e2 = 1;
$errors = 1;
}
if ($rr ne "s" && $rr ne "d" && $rr ne "k" && $rr ne "m")
{
$e3 = 1;
$errors = 1;
}
if ( $rnd eq 0 || ($rnd gt 7 ))
{
$e4 = 1;
$errors = 1;
}
if ( $rs eq "")
{
$e5 = 1;
$errors = 1;
}
if ( $re le 65.00 || ($re gt 490.00))
{
$e6 = 1;
$errors = 1;
}
if ( $rp le 1 || ($rp gt 4 ))
{
$e7 = 1;
$errors = 1;
}
if ($rd le 5 || $rd gt 99)
{
$e8 = 1;
$errors = 1;
}
if ( $errors == 0 )
{
print "<html><body>Thank you for making your reservation</body></html>";
}
else
{
if ($e1 == 1)
{
print "<br>Reservation Number May Not Be Blank<br>";
}
if ($e2 == 1)
{
print "<br>Reservation Create Date may not be blank<br>";
}
if ($e3 == 1)
{
print "<br>The Room Type Must be leters S,D,K,or M<br>";
}
if ($e4 == 1)
{
print "<br>The number of days must be greate than zero and less than 7<br>";
}
if ($e5 == 1)
{
print "<br>The Start Date Must Not be Blank<br>";
}
if ($e6 == 1)
{
print "<br>The Room cost must be between $65.00 and $490.00<br>";
}
if ($e7 == 1)
{
print "<br>The payment method must be 1 thru 4<br>";
}
if ($e8 == 1)
{
print "<br>The discount code must be two digits and be on of the following
numbers 5,10,17,20<br>";
}
}
答案 0 :(得分:2)
它适用于我的测试。我唯一能想到的是你的参数名称并不是你所说的。您还没有向我们展示您的HTML表单,因此我无法确切知道。
你的代码很奇怪。您正在使用字符串比较运算符(gt
,le
)来比较数字。而且您的错误消息似乎与您正在进行的比较相匹配。
此外,您的错误检查技术相当笨重。下面的版本使用较少的变量,可能更易于维护。
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw(:standard);
print header;
my $rn = param("Reservation Name");
my $rc = param("Reservation Create Date");
my $rr = param("Reservation Room Type");
my $rnd = param("Reservation Number of Days");
my $rs = param("Reservation Start Date");
my $re = param("Reservation Room Cost");
my $rp = param("Reservation Payment Method");
my $rd = param("Reservation Discount Code");
my @errors;
if ( $rn eq "") {
push @errors, 'Reservation Number May Not Be Blank';
}
if ( $rc eq "") {
push @errors, 'Reservation Create Date may not be blank';
}
my %valid_rr = map { $_ => 1 } qw[s d k m];
if ( !$valid_rr{$rr} ) {
push @errors, 'The Room Type Must be leters S,D,K,or M';
}
if ( $rnd == 0 || ($rnd > 7 )) {
push @errors, 'The number of days must be greate than zero and less than 7';
}
if ( $rs eq "") {
push @errors, 'The Start Date Must Not be Blank';
}
if ( $re <= 65.00 || ($re > 490.00)) {
push @errors, 'The Room cost must be between $65.00 and $490.00';
}
if ( $rp <= 1 || ($rp > 4 )) {
push @errors, 'The payment method must be 1 thru 4';
}
if ($rd <= 5 || $rd > 99) {
push @errors, 'The discount code must be two digits and be on of the following
numbers 5,10,17,20';
}
if ( @errors ) {
print map { "<br>$_<br>" } @errors;
} else {
print "<html><body>Thank you for making your reservation</body></html>";
}
您还应该重新检查有效字符串的检查。启用use warnings
后,您将获得大量&#34;使用未初始化的值&#34;错误。