我正在为图书数据库做 Perl 作业。我需要制作和 html 表单,用户将在其中键入输入,例如:
1)标题 2)作者 3)语言 4)年份 5)销售额
然后我需要 CGI 来处理它。并存储到数据库“书籍”中。
我正在使用 MySql 数据库。
我面临的问题是没有输出我输入的值,我尝试了很多方法调试它,但仍然没有结果。
这是我的 html 表单“CreateForm.html”
<form method="post" action="http://localhost:8080/cgi-bin/create.cgi" >
<table border="1">
<tr>
<td>Title: </td>
<td><input type="text" name="title"></td>
</tr>
<tr>
<td>Author: </td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td>Language: </td>
<td><input type="text" name="language"></td>
</tr>
<tr>
<td>Year of Publication: </td>
<td><input type="text" name="year"></td>
</tr>
<tr>
<td>Estimated sales: </td>
<td><input type="text" name="sales"></td>
</tr>
<tr>
<td><input type="submit" /></td>
</tr>
</table>
</form>
我在 Windows 上运行 XAMPP
这里基本上是我的代码'create.cgi'
#!"C:\xampp\perl\bin\perl.exe"
print "Content-type: text/html\r\n\r\n";
#include section
use strict;
use DBI;
use CGI;
my $q=new CGI;
#declaration section
my $name;
my $value;
my $sth;
my %FORM;
my $dbh;
my $sql;
my $rv;
my $key;
my $buffer;
my @pairs;
my $pair;
my $title;
my $author;
my $language;
my $year;
my $sales;
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} else {
$buffer = $ENV{'QUERY_STRING'};
}
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
#parsing data
$title=$q->param('title');
$author=$q->param('author');
$language=$q->param('language');
$year=$q->param('year');
$sales=$q->param('sales');
#database connectivity
$dbh = DBI->connect('DBI:mysql:books', 'root', ''
) || die "Could not connect to database: $DBI::errstr";
#test insert
$sth=$dbh->do('INSERT INTO BOOKS (TITLE,AUTHOR,LANGUAGE,YEAR,SALES) VALUES (
"Nineteen eight four",
"George Orwell",
"English",
1947,
25000000)
');
$sql="INSERT INTO BOOKS (TITLE,AUTHOR,LANGUAGE,YEAR,SALES) values ('$title','$author','$language','$year','$sales')";
$sth=$dbh->prepare($sql) or die "can't prepare $sql: $dbh->errstrn";
$rv=$sth->execute;
if ($rv==1){
print "Record has been successfully created !!!<br>";
}else{
print "Error!!while inserting record\n";
exit;
}
$sth = $dbh->prepare("SELECT * FROM BOOKS");
$sth->execute();
foreach $key(sort keys %FORM){
print "<h3>$key: $FORM{$key}</h3>";
}
print "<html>";
print "<head>";
print "<title>Create Form</title>";
print "</head>";
print "<body>";
print "Title: $FORM{'title'} <br/>";
print "Author: $FORM{'author'} <br/>";
print "Language: $FORM{'language'} <br/>";
print "Year: $FORM{'year'} <br/>";
print "Sales: $FORM{'sales'} <br/>";
foreach $key(sort keys %FORM){
print "<h3>$key: $FORM{$key}</h3>";
}
#retrieving all tables to check
#while (my $ref = $sth->fetchrow_hashref()) {
#print "<br>Found a row:\n
# id = $ref->{'BOOKID'},
# tname = $ref->{'TITLE'},
# author: $ref->{AUTHOR},
# language: $ref->{'LANGUAGE'},
# year: $ref->{'YEAR'},
# sales: $ref->{'SALES'} \n";
#}
#while (my $ref = $sth->fetchrow_hashref()) {
#
# %rec=%{$pRec};
# print "$rec{'title'}";
#}
print "</body>";
print "</html>";
$sth->finish();
$dbh->disconnect();
线
foreach $key(sort keys %FORM){
print "<h3>$key: $FORM{$key}</h3>";
}
不输出任何东西
我收到的输出
Record has been successfully created !!!
Title:
Author:
Language:
Year:
Sales:
空条目,但已执行 INSERT 操作
30 Nineteen eight four George Orwell English 1947 25000000
提前致谢