简体   繁体   English

CGI不处理html表单-Perl

[英]CGI not processing html form - Perl

I am doing my Perl Assignment for the book database. 我正在为图书数据库进行Perl分配。 I am required to make and html form that, where the user will type input such as: 我需要制作和html表单,用户将在其中键入输入,例如:

1)title 2)author 3)language 4)year 5)sales 1)标题2)作者3)语言4)年5)销售

then i need CGI to process it. 那么我需要CGI来处理它。 and store into database 'books'. 并存储到数据库“书”中。

I am using MySql database. 我正在使用MySql数据库。

The problem I am facing it is not outputting the values I have entered, I tried many ways debugging it, but still no results. 我面临的问题是不输出我输入的值,我尝试了许多调试它的方法,但仍然没有结果。

Here is my html form 'CreateForm.html" 这是我的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> 

I am running XAMPP on windows 我在Windows上运行XAMPP

Here is basically my code 'create.cgi' 这基本上是我的代码“ 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();

the line 线

foreach $key(sort keys %FORM){
print "<h3>$key: $FORM{$key}</h3>";
}

doesn't output anything 什么都不输出

the output I receive 我收到的输出

Record has been successfully created !!!
Title:
Author:
Language:
Year:
Sales:

Empty entries, however the INSERT operation has been performed 空条目,但是已执行INSERT操作

30  Nineteen eight four     George Orwell   English     1947    25000000

Thanks in Advance 提前致谢

I don't understand why you're messing around with the %FORM hash, considering you are correctly accessing the parameters elsewhere, such as: 考虑到您正确访问了其他地方的参数,例如,我不明白为什么要弄乱%FORM哈希:

$title=$q->param('title');

Therefore, why don't you just output $title ? 因此,为什么不只输出$title呢?

print "Title: $title <br/>";

If you want a hash with the keys and values of the parameters, then get it into a hashref with: 如果要使用参数的键和值进行哈希处理,请使用以下命令将其放入hashref中:

$params = $q->Vars;

Then, if you want to iterate over that hashref, do something like this: 然后,如果要遍历该hashref,请执行以下操作:

foreach my $key ( sort keys %$params ) {
  print "$key has a value of $params->{$key}\n";
}

------------------------ 8< ------------------------ ------------------------ 8 <------------------------

Addendum, not related to the question: 附录,与以下问题无关:

You really don't want to put your values in your SQL the way you are doing it: 真的不想像这样做那样将值放在SQL中:

$sql="INSERT INTO BOOKS (TITLE,AUTHOR,LANGUAGE,YEAR,SALES) values ('$title','$author','$language','$year','$sales')";

Instead, you want to use placeholders and pass in the parameters to the execute() method: 相反,您想使用占位符并将参数传递给execute()方法:

$sql = "INSERT INTO BOOKS (TITLE,AUTHOR,LANGUAGE,YEAR,SALES) values (?,?,?,?,?)";
$sth = $dbh->prepare($sql) or die "can't prepare $sql: $dbh->errstrn";
$sth->execute( $title, $author, $language, $year, $sales );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM