用perl cgi在浏览器中显示值
我能够从数据库中获取无法在浏览器中显示的数据。 下面是代码-用perl cgi在浏览器中显示值,perl,cgi,Perl,Cgi,我能够从数据库中获取无法在浏览器中显示的数据。 下面是代码- my $q = CGI->new; print $q->header,$q->start_html('testing'); my $title = $q->param('title'); my $perl = ""; #these is displayed properly print "<font color=blue><b>TITLE:\"$title\"</b><
my $q = CGI->new;
print $q->header,$q->start_html('testing');
my $title = $q->param('title');
my $perl = "";
#these is displayed properly
print "<font color=blue><b>TITLE:\"$title\"</b><br>";
print "<font color=blue><b>SCRIPT:\"$title\"</b>\n";
my $dbh = DBI->connect("DBI:ODBC:test","username","password") || die "Connection error: $DBI::errstr\n";
my $sql = "select * from tablename where title = '$title'";
my $sth = $dbh->prepare($sql);
$sth->execute;
my @row = $sth->fetchrow_array;
for(my $i=1;$i<=@row;$i++)
{
if($i == 5)
{
$perl = "$row[$i]";
}
}
#below is not displayed in browser
print $q->strong($title);
print $q->strong($perl);
$sth->finish();
$dbh->disconnect;
print $q->end_html;
我只想在浏览器中打印$title和$perl的值。
此程序运行正常,但无法显示$title和$perl的值。我不清楚失败的原因,但在执行查询时应使用:
my $sql = "select * from tablename where title = ?"; # placeholder
my $sth = $dbh->prepare($sql);
$sth->execute($sql); # $sql is used here
占位符是问号?。这将确保正确引用您的值,并防止注入攻击。使用来自CGI对象的数据而不进行清理是非常危险的
而且,您似乎只从数组中获取一个值,因此不需要首先使用循环。你可以这样做:
my $row = $row[5];
要查看该值是否在数据库中,可以使用if defined$row或if@row>=6。请注意,数组从0开始,因此索引为5的元素实际上是第6个元素。只需指出这一点,因为您从1开始循环。尝试直接从命令行运行它,而不使用浏览器 见和 如果以以下方式启动Perl调试器,也可以使用它:
perl -d yourprogram
@mpapec-手动生成的字体元素正在工作,所以必须已经完成。我不明白如何打印标题:\$TITLE\;工作但打印$q->strong$标题;不会,因为它们使用相同的变量。但此代码极易受到SQL注入和XSS攻击。@mpapec我将在运行cgi文件时通过cmd获取值尝试手动打印行,以查看查询是否正常工作:使用Data::Dumper;打印转储文件\@行;