perl - 如何使用 perl 脚本将表单数组从 URL 插入到 DBD 数据库?

标签 perl postgresql psql

我正在尝试使用 perl 脚本将 html 中的数据填充到 DBD 数据库。

这是我的 html 代码:

<html>
</head>


<body>
<H1>Friend's contact book</H1>

<FORM ACTION="contact.pl" METHOD="POST">


<HR>
<H2>Friend's contact</H2>

<PRE>
          Name:<INPUT TYPE="text" NAME="name"> 
         Address:<INPUT TYPE="text" NAME="add"> 


</PRE>

</HR><P>
<INPUT TYPE="submit" VALUE="Sign Up"> or <INPUT TYPE="reset" VALUE="Cancel">
</P>
</FORM>
</body></html>

这是我的 perl 脚本: 联系人.pl

#!/usr/bin/perl

# Initialize DBI.
use DBI;
use strict;

# Make the database connection.
my $dbh = DBI->connect("dbi:Pg:dbname=friendcontact") 
or die my $DBI::errstr;

   # Store the SQL query

myy $stat = my $dbh->prepare("INSERT into friend (name, add) VALUE 
(?,?)");

   # Execute the query

my $stmh->execute();

   # Tidy up

my $stmh->finish();
my $dbh->commit or die my $DBI::errstr;

我无法运行 contact.pl 代码 它说“内部服务器错误”

无论如何我可以纠正这个问题吗?

提前致谢

谢谢 我已经按照你说的编辑了代码,但它有错误 更新后: 内部服务器错误

The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, and inform them of the time the error occurred, and anything you might have done that may have caused the error.

Premature end of script headers: /home/friendcontact/private/cgi-bin/contact.pl

最佳答案

首先,您需要将 Perl 错误定向到浏览器以便进行调试。这是通常的方式,尽管如果不查看代码的 CGI 部分就很难知道:

use CGI::Carp qw(fatalsToBrowser);

您可能还需要更改服务器上的设置。

除此之外,这里还有几个问题。我相信这需要 VALUES 而不是 VALUE:

$stat = $dbh->prepare( "INSERT into friend (name, add) VALUE 
(?,?)");

此外,这是不正确的:

$stmh->execute('$name','$add');

? 占位符与 DBH 一起使用时,您不需要引用变量;它在内部为您处理。即使考虑到这一点,您也使用了单引号,这意味着您没有传入变量,而只是文字字符串 '$name''$add' .只需这样做:

$stmh->execute($name,$add);

这只是一个开始;这里可能还有更多。

更新:您还会遇到错误,因为您在使用变量之前没有声明它们。您应该使用 my $var; 的形式声明每个变量。这使它成为一个词法变量,而不是一个全局变量。这是一个很好的做法,因为您说 use strict;(一件非常好的事情),这也是一个必需的做法。

关于perl - 如何使用 perl 脚本将表单数组从 URL 插入到 DBD 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12710535/

相关文章:

perl - 使用 perl 应用补丁文件?

node.js - Node postgres : Not working issuing a NOTIFY job from postgres CLI

sql - Postgresql按乱序排序

arrays - 如果满足条件,则向数组添加值

perl - 通过 perl 脚本在文件中替换

perl - 在 Perl 中并行读取 2 个文件时的性能

linux - 脚本自动化 postgres 设置

ruby-on-rails - Rails 的 AWS CodeBuild 进程无法安装 pg gem

python - 将整个文件作为一个字符串导入

go - 我怎样才能在 golang gorm 中与 self 建立多对多的关系?