从数据库中删除和更新数据 [英] Deleting and updating data from database
本文介绍了从数据库中删除和更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个连接到数据库的代码,并且我想使用同样的更新按钮从数据库中删除数据。但我只能在表中显示数据并且不能删除。
my $ q = new CGI;
print $ q-> header;
print $ q-> start_html(
-title =>,
);
#print $ q-> start_form;
## mysql用户数据库名称
my $ db =people;
## mysql数据库用户名
my $ user =root;
## mysql数据库密码
my $ pass =;
##用户主机名:这应该是本地主机,但它也可以是不同的
my $ host =127.0.0.1;
## SQL查询
my $ query =select ID,Name,Surname,Gender from person;
$ b $ my $ dbh = DBI-> connect(DBI:mysql:$ db:$ host,$ user,$ pass);
my $ sqlQuery = $ dbh->准备($查询)
或死无法准备$ sqlQuery:$ dbh-> errstr\\\
;
my $ rv = $ sqlQuery->执行
或死亡无法执行查询:$ sqlQuery-> errstr;
print start_form(-method =>'post',-action =>modify.pl);
我的@aRows;
while(my @data = $ sqlQuery-> fetchrow_array()){
my $ cRowId = hidden('ID',$ data [0]);
my $ bt1 = submit('action','delete');
my $ bt2 = submit('action','update');
push @aRows,($ cRowId,$ q-> Tr($ q-> td([$ data [1],$ data [2],$ data [3],$ BT1,BT2 $)));
}
print $ q-> table({ - border =>'1',-align =>'center',-width =>'100%'} (['Name','Surname','Gender','Delete','Update',])]),
($ b $ q-> Tr @aRows,
);
print $ q-> input({ - type =>'button',-class =>'button',-onclick =>window.location.href ='insert。 pl';, - value =>'Shto'});
print $ q-> end_form;
print $ q-> end_html;
delete.pl
使用CGI;
使用CGI qw(标准);
使用DBI;
使用CGI :: Carp qw(set_die_handler);
使用CGI qw /:all /;
BEGIN {
sub handle_errors {
my $ msg = shift;
打印content-type:text / html\\\
\\\
;
#proceed发送电子邮件给系统管理员,
#向浏览器和/或日志写入详细消息,
#etc ....
}
set_die_handler(\& handle_errors);
}
my $ q = CGI-> new();
my $ db =people;
my $ user =root;
my $ pass =;
my $ host =127.0.0.1;
my $ dbh = DBI-> connect(DBI:mysql:$ db:$ host,$ user,$ pass); ('delete'){
my $ row_id = $($ action $)$($ action $) Q-> PARAM( 'ID');
my $ sth = $ dbh-> prepare(DELETE FROM person WHERE ID = $ row_id)or dieCan not prepare $ query:$ dbh-> errstr\\\
;
my $ rv = $ sth-> execute()或者$ DBI :: errstr;
打印已删除;
my $ sth-> finish();
my $ dbh->提交或者死亡$ DBI :: errstr;
}}
我不知道问题出在哪里 p>
解决方案
绝大多数Perl CGI问题都可以通过以下方法来解决:
use strict
和使用警告
I have created a code that connects to database and i want to delete data from database using a button the same for update. but i just can display data in a table and cant delete.
my $q= new CGI;
print $q->header;
print $q-> start_html(
-title => "",
);
# print $q->start_form;
## mysql user database name
my $db = "people";
## 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 ID,Name,Surname,Gender from person";
my $dbh = DBI->connect("DBI:mysql:$db:$host", $user, $pass);
my $sqlQuery = $dbh->prepare($query)
or die "Can't prepare $sqlQuery: $dbh->errstr\n";
my $rv = $sqlQuery->execute
or die "can't execute the query: $sqlQuery->errstr";
print start_form (-method => 'post', -action => "modify.pl" );
my @aRows;
while (my @data = $sqlQuery->fetchrow_array()) {
my $cRowId = hidden('ID', $data[0]);
my $bt1 = submit('action','delete');
my $bt2 = submit('action','update');
push @aRows, ($cRowId, $q->Tr($q->td([$data[1], $data[2], $data[3],$bt1,$bt2])));
}
print $q->table({-border =>'1', -align =>'center', -width => '100%'},
$q->Tr([$q->th([ 'Name', 'Surname', 'Gender', 'Delete', 'Update', ])]),
@aRows,
);
print $q->input({-type => 'button', -class => 'button', -onclick => "window.location.href='insert.pl';", -value => 'Shto'});
print $q->end_form;
print $q->end_html;
delete.pl
use CGI;
use CGI qw(standard);
use DBI;
use CGI::Carp qw(set_die_handler);
use CGI qw/:all/;
BEGIN {
sub handle_errors {
my $msg = shift;
print "content-type: text/html\n\n";
#proceed to send an email to a system administrator,
#write a detailed message to the browser and/or a log,
#etc....
}
set_die_handler(\&handle_errors);
}
my $q = CGI->new();
my $db = "people";
my $user = "root";
my $pass = "";
my $host="127.0.0.1";
my $dbh = DBI->connect("DBI:mysql:$db:$host", $user, $pass);
my $action = $q->param('action'){
given ($action){
when('delete'){
my $row_id = $q->param('ID');
my $sth = $dbh->prepare("DELETE FROM person WHERE ID = $row_id ") or die "Can't prepare $query: $dbh->errstr\n";
my $rv = $sth->execute() or die $DBI::errstr;
print "deleted";
my $sth->finish();
my $dbh->commit or die $DBI::errstr;
}
} }
I dont know where may be the problem
解决方案
The vast majority of Perl CGI problems can be solved by:
- Adding
use strict
anduse warnings
to your code - Fixing all of the errors that now appear in your error log
这篇关于从数据库中删除和更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文