2013-07-24 59 views
1

我正在做一個連接到數據庫'人'的代碼,從那裏獲取數據,我需要做的是用一個按鈕來獲取id點擊此按鈕的人員並刪除或更新。問題是我不知道如何使這個我Perl,因爲在其他語言我做到了。如何添加一個按鈕,從數據庫中獲取數據的id並執行一個動作

my $q= new CGI; 

print $q->header; 
print $q-> start_html(
    -title => "Main", 
    -style => {-src =>'/media/css/ui-lightness/jquery-ui-1.10.3.custom.css" rel="stylesheet' }, 
    -script => [ 
     { -src=>'/media/js/jquery-1.9.1.js'}, 
     { -src=>'/media/js/jquery-ui-1.10.3.custom.js' } 
    ] 
); 

print $q->start_form; 
print $q->table({}, 
     $q->Tr(
      $q->th('Name', 'Surname', 'Age') 
     )); 


# Connect to the database 

## mysql user database name 
my $db = "student"; 
## mysql database user name 
my $user = "root"; 

## mysql database password 
my $pass = ""; 

## user hostname : This should be "localhost" but it can be diffrent too 
my $host="127.0.0.1"; 

## SQL query 
my $query = "select Name,Surname,Age from student"; 

my $dbh = DBI->connect("DBI:mysql:$db:$host", $user, $pass); 
my $sqlQuery = $dbh->prepare($query) 
or die "Can't prepare $query: $dbh->errstr\n"; 

my $rv = $sqlQuery->execute 
or die "can't execute the query: $sqlQuery->errstr"; 

while (my ($Name, $Surname, $Age) = $sqlQuery->fetchrow_array()) { 
    print STDOUT "$Name $Surname $Age"; 
     $q->button(print $q->button(
     -id  => 'leletebtn', 
     -name  => 'submit_form', 
     -value => 'Delete', 
     )   
    ) 
} 

print $q->end_form; 
print $q->end_html; 

回答

0

這裏有很多教程。你必須使用DBI:

http://oreilly.com/catalog/perldbi/chapter/ch04.html

http://www.perl.com/pub/1999/10/DBI.html

my $lastname = 'test'; 

my $dbh = DBI->connect('DBI:Oracle:people') 
       or die "Couldn't connect to database: " . DBI->errstr;#connect 
my $sth = $dbh->prepare('SELECT id,uid FROM people WHERE lastname = ?') 
       or die "Couldn't prepare statement: " . $dbh->errstr;#prepare 
$sth->execute($lastname); # Execute the query 
while (my $ref = $sth->fetchrow_hashref()) { 
    print "$$ref{'id'} \t $$ref{'uid'}\n"; 
} 
+0

它是連接和第一代碼的作品,但delete.pl沒有。如果你找到錯誤的地方,請嘗試編輯我的代碼。我是新的perl – Armida

+0

「你必須使用DBI」 - 這當然是一個非常好的主意,但我不認爲你絕對*有:*: –

+0

你絕對應該使用DBI,除非你知道你在做什麼? :-) – user1126070

相關問題