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;
它是連接和第一代碼的作品,但delete.pl沒有。如果你找到錯誤的地方,請嘗試編輯我的代碼。我是新的perl – Armida
「你必須使用DBI」 - 這當然是一個非常好的主意,但我不認爲你絕對*有:*: –
你絕對應該使用DBI,除非你知道你在做什麼? :-) – user1126070