2016-08-29 61 views
0

我试图制作一个地址簿,它将表单中的用户输入的数据发送到数据库中的表中。我不知道我是否正确地做了这件事,因为我没有找到一个完整的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),我不知道这是否重要。谢谢!

+1

如果您在Godaddy托管帐户上运行它,则需要在Godaddy计算机上安装该模块,而不是您自己的。 – Quentin

+0

https://uk.godaddy.com/help/can-i-add-perl-modules-to-my-hosting-account-1488 – Quentin

+0

您无法从CGI代码发送HTTP状态行。您可以使用“状态”标题设置状态,但如果您未指定,则默认为“200”。要明确地发送,你应该'打印'Status:200 OK \ n“'但是没有必要,唯一需要的头部是'Content-Type',后面应该跟两个*换行符,而不是三个。 – Borodin

回答

-1

你得到的错误是说DBD :: mysql没有安装在你正在运行的perl的实例期待它。你知道你是否使用ActiveState Perl,Strawberry Perl或其他发行版?

使用ActiveState,最好的方法是使用预编译包的存储库:ppm。安装模块的命令是这样的:

ppm install DBD::mysql 

如果你正在使用Perl的草莓,然后看到这个question

+0

我正在使用ActiveState - 当我在命令提示符中输入该命令时,它表示没有缺少要安装的软件包。当我查看Perl Package Manager时,我可以在列表中看到DBD-mysql。除了我的.pl文件中的“use”语句之外,是否还有一些特殊的操作需要将该软件包放入我的godaddy web根文件夹中? – TonyaL

+0

@TonyaL当你在位于_where_的PPM看? –

+0

在软件包列表中 - 我甚至还标记并安装了它。我打电话给godaddy,他们说如果我切换到Linux类型的帐户,它应该解决问题。他们删除了旧帐户,开始了新帐户,重新上传了所有内容(并更改了主机并在代码中登录了信息)。我不再发生这个错误 - 但现在我得到一个500内部错误消息。头 - >桌子。 – TonyaL

相关问题