【发布时间】: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->header打印您的Content-Type标头。
标签: html mysql database forms perl