gpt4 book ai didi

html - Perl - 将用户输入的 cgi 表单数据传递给 perl 程序,然后将其放入 mysql 数据库

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:52 25 4
gpt4 key购买 nike

我曾尝试在论坛中搜索解决方案,但大多数答案都太难理解了。所以我正在为一个小社区制作一个网站,目前我们有我们的数据库和 html 设计布局,但我对如何将我的 Perl CGI 表单推送到另一个 Perl 程序然后改变我的数据库感到困惑.

这是更改数据库表的 Perl Controller (并且它可以工作):

#!/usr/bin/perl -w
#!/usr/bin/perl -wT

# DBI is the standard database interface for Perl
# DBD is the Perl module that we use to connect to the <a href=http://mysql.com/>MySQL</a> database
use DBI;
use DBD::mysql;

use warnings;

#----------------------------------------------------------------------
# open the accessDB file to retrieve the database name, host name, user name and password
open(ACCESS_INFO, "accessDB.txt") || die "Can't access login credentials";


# assign the values in the accessDB file to the variables
my $database = <ACCESS_INFO>;
my $host = <ACCESS_INFO>;
my $userid = <ACCESS_INFO>;
my $passwd = <ACCESS_INFO>;
my $tablename = "Article";

# the chomp() function will remove any newline character from the end of a string
chomp ($database, $host, $userid, $passwd);

# close the accessDB file
close(ACCESS_INFO);
#----------------------------------------------------------------------

# invoke the ConnectToMySQL sub-routine to make the database connection
$connection = ConnectToMySql($database);

if ($tablename == "Article"){
$connection = InsertArticle($database);
}

elsif ($tablename == "Category"){
$connection = InsertCategory($database);

}
elsif ($tablename == "Comment"){
$connection = InsertComment($database);

}
elsif ($tablename == "User"){
$connection = InsertUser($database);

}
else {
print "No such table found. Contact website administrator.\n"

}
sub InsertArticle{

$query = "insert into $tablename (Id, CategoryId, UserId, Title, Content) values(?, ?, ?, ?, ?)";

$statement = $connection->prepare($query);


$statement->execute('undef', '1', '1029', 'Dota2>League', 'textfromarticle');
}
sub InsertCategory{

$query = "insert into $tablename (Id, CategoryId, UserId, Title, Content) values(?, ?, ?, ?, ?)";

$statement = $connection->prepare($query);


$statement->execute('undef', '1', '1029', 'Dota2>League', 'textfromarticle');
}
sub InsertComment{

$query = "insert into $tablename (Id, CategoryId, UserId, Title, Content) values(?, ?, ?, ?, ?)";

$statement = $connection->prepare($query);


$statement->execute('undef', '1', '1029', 'Dota2>League', 'textfromarticle');
}
sub InsertUser{

$query = "insert into $tablename (Id, CategoryId, UserId, Title, Content) values(?, ?, ?, ?, ?)";

$statement = $connection->prepare($query);


$statement->execute('undef', '1', '1029', 'Dota2>League', 'textfromarticle');
}

exit;

#--- start sub-routine ------------------------------------------------
sub ConnectToMySql {
#----------------------------------------------------------------------

my ($db) = @_;

# assign the values to your connection variable
my $connectionInfo="dbi:mysql:$db;$host";

# make connection to database
my $l_connection = DBI->connect($connectionInfo,$userid,$passwd);

# the value of this connection is returned by the sub-routine
return $l_connection;

}

#--- end sub-routine --------------------------------------------------

将来,我将通过全局变量定义数据库中的其他表,这些变量取决于用户在正确网页上按下的按钮。比如,如果他们正在查看文章列表,顶部的选项将是“提交文章”。从那里,CGI 表格将发送给他们,他们可以填写。

这里是生成表单的 CGI,该表单将提交给上述 Controller 脚本以更改表:

#!/usr/bin/perl
#!/usr/bin/perl -wT
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser); #remove this in production

my $q = new CGI;
print $q->header; #Content-Type: text/html; charset=ISO-8859-1
print $q->start_html(
-title => 'submit an Article', #page name
-style => {'src' => '/dmrwebsite/dmrwebsite/userinterface'}, #link to style sheet
);
print $q->start_form(
-name => 'submitting an Article',
-method => 'POST',
enctype => &CGI::URL_ENCODED,
-onsubmit => 'return true',
-action => '/dmrwebsite/dmrwebsite/controller.addtotable.pl',
);
print $q-.textfield(
-name => 'title',
-value => 'default value',
-required,
-size => 20,
-maxlength =>50,
);
print $q->textarea(
-name => 'content',
-value => 'default value',
-required,
-maxlength => 1000,
-cols => 60,
);
print $q->textarea(
-name => 'url',
-value => 'default value',
maxlength => 100,
cols =>60,
);
print $q-checkbox(
-name => 'humancheck',
-checked => 1,
-value => 'two',
-label => 'The number two',
);
print $q-submit(
-name => 'submit_Article',
-value => 'submit Article',
-onsumbit => 'javascript: validate_form()',
);
if ($q->param()) {
submit_form($q);
} else {
print "Please check your form for inaccuracies.";
}
sub submit_form($){
my ($q) = @_;


}
print $q->end_form; #ends the form html
print $q->end_html; #end the html document

所以基本上我所坚持的是理解如何将表单数据发送到 perl 脚本,然后我可以在 my $tablename = "Article";$statement->execute('undef', '1', '1029', 'Dota2>League', 'textfromarticle');.

此外,我没有要发送到参数 -onsubmit => javaapplication(), 的 javascript 程序。那有必要吗?我可以用我自己的 Perl 程序来检查用户输入的字段吗?我将如何调用这个函数?在同一个文件中,还是只能在父目录中,例如/website/perlchecker.pl?

任何帮助将不胜感激,因为我才使用 Perl 几天,更不用说 CGI 和 html 了。不过有几个人在网站的前端帮助我。

谢谢,-奥里

最佳答案

这么多建议...

首先,您的数据库插入程序似乎只是插入固定数据,所以我不确定您认为它是如何工作的。此外,if ($tablename == "Article")(和类似的)行不会执行您想要的操作。您需要使用 eq 而不是 ==

要回答您提出的问题 - 您需要更改数据库程序,以便它接受包含要插入数据库的数据的输入(可能是命令行参数)。然后,您可以在 CGI 程序中添加一行调用此程序的行(可能使用 system()),并在命令行上将 CGI 参数中的数据传递给它。

代码看起来像这样:

my $title   = $q->param('title');
my $content = $q->param('title');
# ... other params ...
system('db_script.pl', 'Article', $title, $content, ...)';

但是请不要那样做。这是个糟糕的主意。

相反,我强烈建议您将数据库操作程序重新编写为一个模块。这样,您可以将模块加载到任何需要与数据库对话的程序中,并通过调用函数而不是调用外部程序来访问数据库。如果由我决定,那么我肯定会使用 DBIx::Class生成这个库 - 但我意识到这很可能被视为相当先进。

然后是房间里的大象。您仍在使用 CGI 来编写 Web 界面。 CGI 模块已从最新版本的 Perl 中删除,因为它不再被认为是编写 Web 应用程序的最佳实践。我建议查看 CGI::Alternatives了解其他更现代的工具。

但是如果您决定继续将您的程序编写为 CGI 程序,那么至少请不要使用 HTML 生成功能。至少十五年来,我们都知道在程序源代码中包含 HTML 是一个糟糕的主意。没有理由在 2015 年仍然这样做。您真的应该使用某种模板引擎将 HTML 与 Perl 代码分开。我推荐 Template Toolkit .

我不确定您是从哪里学习这些技术的,但您的来源似乎比公认的最佳实践落后十年。

关于html - Perl - 将用户输入的 cgi 表单数据传递给 perl 程序,然后将其放入 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31266379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com