On 05/12/2016 08:04 PM, lee wrote:
... I appreciate perl for:


$dbh->do("INSERT INTO $T_ENTRIES (" .
         join(', ', map($dbh->quote_identifier($_), $cgi->param)) . ') VALUES 
(' .
         join(', ', map($dbh->quote($_), map($cgi->param($_), $cgi->param))) . 
')')
   if(scalar($cgi->param) == 111);

not bad but i have a few improvements that you may like.

i would not call $cgi->param so often. easy enough to use arrays.

my @cgi_params = $cgi->param() ;
my @cgi_values = $cgi->param( @cgi_params ) ;

i like to build up the sql parts outside of the call and to use ? placeholders which are quoted for you.

    my $holders = join ',', ('?') x @cgi_params ;

and i like here docs for sql so i can see the sql and not need all those quotes and noise. also assigning the sql to a scalar so i can print it out for debugging

    my $sql = <<SQL ;
INSERT INTO $T_ENTRIES ( $holders ) VALUES ( $holders )
SQL

my $sth = $dbh->prepare( $sql ) ;
$sth->execute( @cgi_params, @cgi_values ) ;

it may look longer but it is easier to read, debug and reuse this way. it can be made into a sub with other options (selecting or where clauses, etc.).

i didn't add in the if condition but that can be put in front of this code.

thanx,

uri





--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to