-3

我想使用 perl CGI 从数据库中删除数据,但它向我显示了错误:全局符号“$queryDelete”需要在 Dbm.pm 第 10 行显示明确的包名称。

package Dbm;
use DBI;
use strict;
use POSIX "fmod";
    sub connection {
         return DBI->connect("DBI:mysql:boys:127.0.0.1" ,'root','');
    };

    sub do_delete {
        my $rData = shift;  
        $queryDelete = " WHERE ";
        my $cWhere = get_condition($rData);
        my $queryDelete="DELETE FROM $rData->{table} $cWhere";      
        my $dbh = connection;
        my $sqlDelete = $dbh->prepare($queryDelete) or die "Can't prepare $queryDelete: $dbh->errstr\n";
        return ($sqlDelete->execute) or die "can't execute the query: $sqlDelete->errstr";
    }
sub get_condition{
    my $rData = shift;
    my $cWhere = " 1=1 ";

    while (my ($key,$value) = each $rData->{condition}) {
        if (ref($value) eq 'ARRAY'){
            $cWhere .= " AND ";

            #print $key.$value->[0];

            my $counter=0;
            foreach my $cValueArray (@{$value}){
                    if(fmod($counter,3)==0) {
                        $cWhere.=" ".$key." ".$cValueArray." ";
                    }
                    else {
                        $cWhere.= " ".$cValueArray. " ";
                    }
                    $counter++;
            }
        }else{
            #print "$key=$value\n

            $cWhere .= " AND ". $key."=". $value;
        }
    };

return $cWhere;
}
1;

测试.pl

 my $q = new CGI();

    my $handle = Dbm::connection();

    sub test_delete{
        my $rData = {
            table     =>'boys',
            condition => {
                ID   => 99,
                Emri => ['<>','\'mon\'','AND','like','\'boy%\''],
            }
        };

        return Dbm::do_delete($rData);
    };
print $q-> start_html(
   -title   => "Main",
   -style   => [ {-src =>'/media/css/ui-lightness/jquery-ui-1.10.3.custom.css', -rel=>'alternate'},
                 {-src =>'/media/css/ui-lightness/mystyle.css', -rel=>'stylesheet', -type=>'text/css'}

   ],
   -script  => [ 
        { -src=>'/media/js/jquery-1.9.1.js'},
        { -src=>'/media/js/jquery-ui-1.10.3.custom.js' }
   ]
);
print test_delete();
4

2 回答 2

3

你正在使用strict. 那挺好的。

sub do_delete {
    my $rData = shift;  
    $queryDelete = " WHERE ";
    my $cWhere = get_condition($rData);
    my $queryDelete="DELETE FROM $rData->{table} $cWhere";      
    ...

在这里,您尝试在使用声明之前$queryDelete使用变量(您将“ WHERE ”分配给它),两行之后。那很糟。您必须在使用变量之前声明它们。my

您可以将my向上移动到您的第一次使用$queryDelete,但这可能不是很有用,因为您只是在两行后为其分配新字符串时覆盖它。

我的猜测是你可能正在尝试做这样的事情:

sub do_delete {
    my $rData = shift;  
    my $cWhere = get_condition($rData);
    my $queryDelete="DELETE FROM $rData->{table} WHERE $cWhere";      
    ...
于 2013-07-29T19:20:29.953 回答
2

您没有在正确的位置定义 $queryDelete 变量。您首先使用它,然后再定义它。尝试:

my $queryDelete = " WHERE ";
...
$queryDelete="DELETE FROM $rData->{table} $cWhere";

代替

$queryDelete = " WHERE ";
...
my $queryDelete="DELETE FROM $rData->{table} $cWhere";
于 2013-07-29T19:17:48.487 回答