我试图制作一个地址簿,它将表单中的用户输入的数据发送到数据库中的表中。我不知道我是否正确地做了这件事,因为我没有找到一个完整的HTML代码和Perl代码的例子 - 只是片段。我收到一个错误(我将在代码后发布)。使用Perl将数据从网页表单发送到MySQL数据库
<!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="[email protected]" 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();
错误:
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are "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. Perhaps the DBD::mysql perl module hasn't been fully installed, or perhaps the capitalisation of 'mysql' isn't right. Available drivers: CSV, DBM, ExampleP, File, Gofer, ODBC, Oracle, Proxy, SQLite, Sponge. at G:\PleskVhosts\tonyalesher.com\httpdocs\project\test2.pl line 7 HTTP/1.0 200 OK Content-Type: text/html ".
我从我的命令提示符下下载的DBD,但我找不到它提的文件夹 - 它在我的电脑上不存在。我正在使用godaddy帐户(plesk),我不知道这是否重要。谢谢!
如果您在Godaddy托管帐户上运行它,则需要在Godaddy计算机上安装该模块,而不是您自己的。 – Quentin
https://uk.godaddy.com/help/can-i-add-perl-modules-to-my-hosting-account-1488 – Quentin
您无法从CGI代码发送HTTP状态行。您可以使用“状态”标题设置状态,但如果您未指定,则默认为“200”。要明确地发送,你应该'打印'Status:200 OK \ n“'但是没有必要,唯一需要的头部是'Content-Type',后面应该跟两个*换行符,而不是三个。 – Borodin