【问题标题】:Sending data from web form to MySQL database with Perl使用 Perl 将数据从 Web 表单发送到 MySQL 数据库
【发布时间】:2017-01-05 22:51:10
【问题描述】:

我正在尝试制作一个地址簿,它获取用户在表单中输入的数据,然后将其发送到数据库中的表中。我不知道我是否做得对,因为我还没有找到完整的 html for 和 perl 代码示例 - 只是 sn-ps。我收到一个错误(我会在代码之后发布)。

<!doctype html>
<html lang="en">
    <head>
      <meta charset="utf-8">
      <title>Address Book</title>
   <link rel="stylesheet" href="stylesheet.css">
 </head>

<body>
<h2>Address Book</h2>
<form action="test2.pl" name="myForm" method="post">
    <fieldset>
        <legend>Contact Details</legend>
        <div>
            <label for="First_Name">First Name:  </label>
            <input type="text" id="First_Name" name="First_Name" placeholder="John" required><br>
        </div>

        <div>
            <label for="Last_Name">Last Name:  </label>
            <input type="text" id="Last_Name" name="Last_Name" placeholder="Doe" required><br>
        </div>

        <div>
            <label for="Address">Address:  </label>
            <input type="text" id="Address" name="Address" placeholder="123 My Street Anytown, US 45678" required><br>
        </div>

        <div>
            <label for="Phone">Phone:  </label>
            <input <input pattern="(?:\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}" id="Phone" name="Phone" placeholder="(xxx)xxx-xxxx" maxlength="13" required> <br>
        </div>

        <div>
            <label for="Birthday">Birthday:  </label>
            <input type="date" id="Birthday" name="Birthday" value="" required><br>
        </div>

        <div>
            <label for="Email">Email:  </label>
            <input type="email" id="Email" name="Email" placeholder="johndoe@email.com" required><br>
        </div>

        <div>
            <label for="Relationship">Relationship:  </label>
            <input type="text" id="Relationship" name="Relationship" placeholder="brother" required><br>
        </div>

        <div>
            <input type="submit" value="Submit">
            <input type="reset" value="Reset">
        </div>
</form>
</body>
</html>

还有perl代码:

use strict;
use warnings;
use DBI;
print "HTTP/1.0 200 OK\n";
print "Content-Type: text/html\n\n\n";

 my $dbh=DBI->connect("DBI:mysql:database=address_book;host=**.**.***.159", "***", "****");
my $o = new CGI;

my $Contact_ID = $o -> param("Contact_ID");
my $First_Name = $o -> param("First_Name");
my $Last_Name = $o -> param("Last_Name");
my $Address = $o -> param("Address");
my $Phone = $o -> param("Phone");
my $Birthday = $o -> param("Birthday");
my $Email = $o -> param("Email");
my $Relationship = $o -> param("Relationship");

$dbh->do("INSERT INTO Contacts VALUES (?, ?, ?, ?, ?, ?, ?, ?)", undef, '0', $First_Name, $Last_Name, $Address, $Phone, $Birthday, $Email, $Relationship);


$dbh->disconnect();

错误:

由于未返回完整的 HTTP 标头集,指定的 CGI 应用程序行为异常。它返回的标题是“install_driver(mysql) failed: Can't locate DBD/mysql.pm in @INC (@INC contains: C:/Program Files (x86)/Parallels/Plesk/Additional/Perl/site/lib C:/Program Files (x86)/Parallels/Plesk/Additional/Perl/lib .) at (eval 3) line 3. 也许 DBD::mysql perl 模块尚未完全安装,或者可能是 'mysql ' 不对。可用的驱动程序:CSV、DBM、ExampleP、File、Gofer、ODBC、Oracle、Proxy、SQLite、Sponge。位于 G:\PleskVhosts\tonyalesher.com\httpdocs\project\test2.pl 第 7 行 HTTP/ 1.0 200 OK Content-Type: text/html ".

我从命令提示符下载了 DBD,但找不到它提到的文件夹 - 我的计算机上不存在它。我正在使用一个godaddy帐户(plesk)我不知道这是否重要。谢谢!

【问题讨论】:

  • 如果您在 Godaddy 主机帐户上运行它,那么您需要在 Godaddy 计算机上安装该模块,而不是您自己的。
  • 您不能从 CGI 代码发送 HTTP 状态行。您可以使用Status 标头设置状态,但如果您不指定,则默认为200。要明确发送,您可以使用print "Status: 200 OK\n",但没有必要,唯一需要的标头是Content-Type,后面应该跟两个换行符,而不是三个。
  • my $o = new CGI 不起作用,因为您没有 use CGI。您可以仅使用 print $o-&gt;header 打印您的 Content-Type 标头。

标签: html mysql database forms perl


【解决方案1】:

您得到的错误是说 DBD::mysql 没有安装在您正在运行的 perl 实例所期望的位置。您知道您使用的是 ActiveState Perl、Strawberry Perl 还是其他发行版?

使用 ActiveState,最好使用它的预编译包存储库:ppm。安装模块的命令类似于:

ppm install DBD::mysql

如果您使用的是 Strawberry Perl,请查看此question

【讨论】:

  • 我正在使用 ActiveState - 当我在命令提示符中输入该命令时,它表示没有缺少要安装的软件包。当我查看 Perl 包管理器时,我可以在列表中看到 DBD-mysql。除了我的 .pl 文件中的“use”语句之外,我还需要做一些特别的事情才能将该包放入我的 godaddy Web 根文件夹吗?
  • @TonyaL 当您查看 PPM 时,在哪里
  • 在软件包列表中 - 我什至还标记了它然后安装了它。我打电话给godaddy,他们说如果我切换到Linux 类型的帐户,它应该可以解决问题。他们删除了旧帐户,启动了新帐户,我重新上传了所有内容(并更改了主机和代码中的登录信息)。我不再收到该错误 - 但现在我收到 500 内部错误消息。头 -> 办公桌。
  • @TonyaL 啊,我明白了你的问题,你是在你的机器上安装 Perl 模块,而不是在你的程序中。将您的程序复制到服务器不会带走模块。您还需要在服务器上安装模块,或者使用App::FatpackerPAR 将您的依赖项与您的程序捆绑在一起。
猜你喜欢
  • 2021-08-21
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-14
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
相关资源
最近更新 更多