[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cgiapp] dbh->quote error I can't see


On Mon, 29 Nov 2004 12:08:25 -0600, Ed Pigg <suppressed> wrote:
> Hi all,
> 
> I've been knocking my head against an error I get from $dbh->quote and
> I'm sure that I'm missing something obvious. I've just been looking at
> the same thing too long. Here's the environment

Michael has already pointed to your problem (that $dbh isn't defined),
but I have a couple of simple suggestions that could simplify your
code and possible save you some development time...

>                 unless ( $error ) {
> #                       $authcode = $dbh->quote( $authcode );
> # ->I tried manually quoting the values to see if it was something in
> DBI, but no luck.
>                         $authcode = "'" . $authcode . "'";
>                         $reqname = "'" . $reqname . "'";
>                         $created_ip = "'" . $created_ip . "'";
>                         my @value_array = ( $authcode, $reqname, $created_ip );
>                         my @insertField_array = qw( authcode username created_ip
> created_date );
>                         my $value;
> # -> This is the original block I had used before moving to
> CGI::Application
> #                       foreach $value ( @value_array ) {
> #                               $value = $dbh->quote( $value );                 # all columns are varchar so
> they must be quoted
> #                       }
>                         push @value_array, 'NOW()';                                     # add the NOW() function to get
> mysql timestamp value in second timestamp column
> 
>                         my $value_list = "(" . join(",", @value_array) . ")";
>                         $value_list =~ /(.*)/;
>                         $value_list = $1;
>                         my $field_list = "(" . join(",", @insertField_array) . ")";
>                         my $sql = "INSERT INTO authorize $field_list VALUES $value_list";
>                         my $sth;
>                         $sth = $dbh->prepare( $sql ) or die "Could not prepare authorization
> insert -$sql- " . $sth->errstr();
>                         $sth->execute() or die "Could not execute authorization insert
> -$sql- " . $sth->errstr();
>                         $sth->finish;

This seems to be a lot of work to just update a couple of values in
the database.  I would recommend using one of the many DBI wrappers
available on CPAN.  See the following article for an overview of the
most popular ones:
http://poop.sourceforge.net/

As an example I'll show you how the above could work if you used Class::DBI.

Create a class for your table:

package MyDB::Authorize;
use base qw(Class::DBI::mysql);
__PACKAGE__->set_up_table('authorize');
1;

And now for the code to update the database:

unless ( $error ) {
  my $authorize = MyDB::Authorize->create({
                     authcode => $authcode,
                     username => $reqname,
                     created_ip => $created_ip,
  }) or die "Could not create authorization record";
}

Your values are automatically quoted for you, and you don't even have
to write another INSERT or UPDATE statement...  Also, you don't need
to pass $dbh around anymore, since Class::DBI worries about that for
you...

>         if ( $error ) {
>                 $session->param( 'error'        => $error );
>                 my $newurl = '../cgi-bin/asep.cgi?rm=error';
>                 $self->header_type('redirect');
>                 $self->header_props( -url => $newurl );
>                 return "Redirecting to $newurl";
>         } else {
>                 return $template->output;
>         }

I seem to do a lot of redirecting, so I created a very simple redirect
function that saves me from remembering how to do it in
CGI::Application.

sub redirect {
  my $self = shift;
  my $location = shift;

  $self->header_add(-location => $location);
  $self->header_type('redirect');
  return '';
}

Now in my runmodes I can just do:

return $self->redirect('../cgi-bin/asep.cgi?rm=error');

Which saves me a few keypresses, and I don't have to bother checking
the docs on how header_type works everytime (I have a short memory ;)

Just a couple of small suggestions that might make life a little bit easier.

Cheers,

--
Cees Hek

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.