HTML代码:
留言本--显示页面
留言本内容显示
[% FOREACH item IN Message %]
留言者:[% item.user %]
留言信息:[% item.info %]
[% END %]
Perl代码:
#!/usr/bin/perl
use DBI;
use CGI ':standard';
use Template;
BEGIN{
use CGI::Carp qw(fatalsToBrowser carpout);
}
my @myarray;
$db=DBI->connect('DBI:mysql:sample_db;user=root');
if (param()){
$username=param('username');
$info=param('info');
$sth=$db->prepare("insert into test(user,info) values(?,?)")
or die "Can't prepare sql statement".DBI->errstr;
$sth->execute($username,$info);
$sth=$db->prepare("select user,info from test ")
or die "Can't prepare sql statement".DBI->errstr;
$sth->execute();
if($sth->rows==0){
print "No date";
exit;
}
while(my @val=$sth->fetchrow_array()){
push @myarray, { user => $val[0], info => $val[1] };
}
$sth->finish();
$db->disconnect();
}
else{
print "Sorry!";
}
my $config = {
INCLUDE_PATH => 'ttdir',
};
my $vars = {
Message => \@myarray,
};
my $input = 'post.tt';
my $tt = Template->new($config)||die "$Template::ERROR\n";
print header(-type=>'text/html',-charset=>'gb2312');
$tt->process($input,$vars) || die $tt->error(), "\n";
阅读(1359) | 评论(0) | 转发(0) |