簡體   English   中英

CGI不處理html表單-Perl

[英]CGI not processing html form - Perl

我正在為圖書數據庫進行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

提前致謝

考慮到您正確訪問了其他地方的參數,例如,我不明白為什么要弄亂%FORM哈希:

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

因此,為什么不只輸出$title呢?

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

如果要使用參數的鍵和值進行哈希處理,請使用以下命令將其放入hashref中:

$params = $q->Vars;

然后,如果要遍歷該hashref,請執行以下操作:

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

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

附錄,與以下問題無關:

真的不想像這樣做那樣將值放在SQL中:

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

相反,您想使用占位符並將參數傳遞給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