2012-10-03 28 views
1

我想使用Perl腳本將數據填充到DBD數據庫中。如何使用perl腳本從URL向DBD數據庫插入表單數組?

這裏是我的html代碼:

<html> 
</head> 


<body> 
<H1>Friend's contact book</H1> 

<FORM ACTION="contact.pl" METHOD="POST"> 


<HR> 
<H2>Friend's contact</H2> 

<PRE> 
      Name:<INPUT TYPE="text" NAME="name"> 
     Address:<INPUT TYPE="text" NAME="add"> 


</PRE> 

</HR><P> 
<INPUT TYPE="submit" VALUE="Sign Up"> or <INPUT TYPE="reset" VALUE="Cancel"> 
</P> 
</FORM> 
</body></html> 

這裏是我的perl腳本: contact.pl

#!/usr/bin/perl 

# Initialize DBI. 
use DBI; 
use strict; 

# Make the database connection. 
my $dbh = DBI->connect("dbi:Pg:dbname=friendcontact") 
or die my $DBI::errstr; 

    # Store the SQL query 

myy $stat = my $dbh->prepare("INSERT into friend (name, add) VALUE 
(?,?)"); 

    # Execute the query 

my $stmh->execute(); 

    # Tidy up 

my $stmh->finish(); 
my $dbh->commit or die my $DBI::errstr; 

我不能運行contact.pl代碼 它說「內部服務器錯誤「

有沒有anywa我可以糾正這個問題嗎?

非常感謝提前

謝謝 我已經編輯的代碼,就像你說的,但它是一種與 錯誤更新後: 內部服務器錯誤

The server encountered an internal error or misconfiguration and was unable to complete your request. 
Please contact the server administrator, and inform them of the time the error occurred, and anything you might have done that may have caused the error. 

Premature end of script headers: /home/friendcontact/private/cgi-bin/contact.pl 

回答

0

你不」 t do準備好的句柄。剛準備和執行:

$stat = $dbh->prepare("INSERT into friend (name, add) VALUES(?,?)"); 
$stat->execute($name, $add); 

而且我假設你有其他代碼設置$name$add變量,因爲它不會自動發生。

編輯:您還有其他問題。不要在致電execute時引用變量。而其VALUES在INSERT語句

+0

非常感謝 我已經編輯成有,但仍然有錯誤 – userpane

1

首先,你需要獲得定向到瀏覽器,以便您可以調試你的Perl的錯誤。這是通常的方式,雖然很難知道沒有看到您的代碼的CGI部分:

use CGI::Carp qw(fatalsToBrowser); 

您也可能需要更改您的服務器上的設置。

除此之外,這裏還有幾個問題。我相信這需要VALUES而不是VALUE

$stat = $dbh->prepare("INSERT into friend (name, add) VALUE 
(?,?)"); 

此外,這是不正確的:

$stmh->execute('$name','$add'); 

當使用?佔位符胸徑,你並不需要引用您的變量;它在內部爲你處理。即使允許這樣做,你也使用單引號,這意味着你沒有傳入變量,而只是字符串'$name''$add'。只需做到這一點:

$stmh->execute($name,$add); 

這只是一個開始;這裏可能更多。

更新:由於在使用它們之前尚未聲明變量,因此您也會遇到錯誤。您應該使用表格my $var;來聲明每個變量。這使得它成爲一個詞彙變量,而不是全局變量。這是一個很好的做法,因爲你說use strict;(這是一件很好的事),這也是一種必要的做法。

+0

非常感謝 如你所說 和上市瀏覽器 – userpane

+0

給出的錯誤列表我曾編輯,但仍然有問題 – userpane

+0

@userpane,更新,說明你的錯誤信息。 – dan1111

相關問題