从数据库中删除最后一个条目

时间:2012-03-01 23:54:01

标签: mysql perl

我在Perl中有一个CGI脚本。它是较大程序的一部分。在这部分程序中,我要求用户输入名称,并根据名称从数据库中删除相应的条目。方法正在删除数据库中除最后一行之外的所有行。请查看代码并建议任何更改。

#!/usr/bin/perl

use warnings;
use strict;
use CGI ':standard';
use DBI;
if(param())
{
    my @params=param();
    my $name=param('name')||'';
    #my $number=param('number')||'';

    my $start = [ Time::HiRes::gettimeofday( ) ];

    my $username="root";
    my $password="";
    my $dbh=DBI->connect("DBI:mysql:sample","root","");
    my $sth=$dbh->prepare("DELETE FROM sample2 WHERE name='$name'");
    $sth->execute or die"Cannot execute sth:$DBI::errstr";
    $dbh->disconnect();

    print
    header(),
    start_html(
        -title =>'Welcome',
        -text =>'#520063'
    ),
    h1("The record has been deleted"),
    end_html();


    my $elapsed = Time::HiRes::tv_interval( $start );
    print "Elapsed time: $elapsed seconds!\n";
}
else
{
    print
    header(),
    start_html('A Simple Form'),
    h1('Please Enter the name(As in the record) you want to delete '),
    start_form(),
    'Name: ',
    textfield(-name=>'name'),
    br(),
    #'Phone Number: ',
    #textfield(-name => 'number'),
    #br(),
    submit(),
    end_form(),
    end_html();
}

0 个答案:

没有答案