--- Lawrence Statton <[EMAIL PROTECTED]> wrote:
> > --0-551411304-1121705388=:507
> > Content-Type: text/plain; charset=iso-8859-1
> > Content-Transfer-Encoding: 8bit
> >
> > Hi all,
> >
> > I'm getting an error when trying to do an INSERT
> statement to a MySQL databas
> > e. There's something I'm not understanding here.
> Can anyone point me in the r
> > ight direction? I also tried a "do" method, but
> got the same error. I know th
> > e "param" function is loading the values from the
> form, because I've used a "
> > print" statement to check the variables.
> >
> > TIA
> > Ron
> >
> >
> ---------------------snip---------------------------
> > Software error:
> > Can't call method "execute" on an undefined value
> at C:/www/cgi-bin/load_comp
> > any_products.cgi line 21.
> > For help, please send mail to the webmaster
> ([EMAIL PROTECTED]), giving this er
> > ror message and the time and date of the error.
> >
> ---------------------snip---------------------------
>
> 1) Your first mistake is never checking for any
> errors;
>
> my $dbh = DBI->conect(...) or
> die "...helpful error message...";
I was using:
my $dbh = DBI->connect("DBI:mysql:company",
"geeksatlarge") or die "Error: $DBI::errstr\n";
but will try:
die "no database: ", DBI->errstr unless $DBH;
Thanks!
>
> Perhaps ESPECIALLY interesting would be around line
> 20 adding
> something of the form
>
> my $sth = $dbh->prepare($sql) or
> die "There is no statement handle because: ",
> $dbh->errstr;
>
Yep! I saw a lot of mail on this one. I'm removing the
extra "my". It was an oversight. :-)
> 2) Use placeholders. REALLY -- where the [EMAIL PROTECTED] are
> people learning to
> write SQL?
>
> 3) I have a personal bias against the format of
> insert you have used,
> and it makes it utterly impossible to help debug
> your code because
> now a critical piece of data ( the output of a
> DESCRIBE products)
> is invisible to us.
>
> I always prefer the "INSERT INTO table ( col,
> col, col ) VALUES (
> ?, ? , ? ) ; form as it handles much more
> gracefully the addition
> of columns in a table.
Yes, the though had crossed my mind to use this
format, so I'm switching over; also, using
placeholders.
I won't be able to test any corrections until tonight
or tomorrow though.
Thanks, to everyone on the list for help while I'm in
Perl/CGI infancy. ;-)
Ron Smith
> #!/www/perl/bin/perl -wT
> use strict;
> use DBI;
> use CGI qw(:standard);
> use CGI::Carp qw(fatalsToBrowser);
> my $sku = param('sku');
> my $partNum = param('partNum');
> my $name = param('name');
> my $descr = param('descr');
> my $stockNum = param('stockNum');
> my $qty = param('qty');
> my $img = param('img');
> my $vendNum = param('vendNum');
> my $price = param('price');
> my $dbh = DBI->connect("DBI:mysql:company",
> "username", "password") or die "Error:
> $DBI::errstr\n";
> my $sql = "INSERT INTO products VALUES ('$sku',
> '$partNum', '$name', '$descr', '$stockNum', '$qty',
> '$img', 'vendNum', '$price')";
> my $sth = $dbh->prepare($sql);
> my $sth->execute; <=============line 21
> $dbh->disconnect;
>
>
> So, let's build a test script that includes all of
> the things I
> mentioned.
> -------------------------- cut here
> ------------------------
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Data::Dumper;
> use DBI;
>
> our $DBH = DBI->connect( 'dbi:mysql:dbname=test',
> undef , undef );
>
> die "no database: ", DBI->errstr unless $DBH;
>
>
> # version for running in CGI
> #my ($sku, $partNum, $name, $descr, $stockNum,
> # $qty, $img, $vendNum, $price) = map { param($_)
> } qw / sku partNum name descr stockNum qty img
> vendNum price /;
>
>
> #
> # brute force for testing
> #
> my $sku = '123456';
> my $partNum = '501-1627';
> my $name = 'TGX Frame Buffer';
> my $descr = 'Sun TGX frame buffer; CG6';
> my $stockNum = 'A-102-55';
> my $qty = 10;
> my $img = undef;
> my $vendNum = '5011627';
> my $price = '12.75';
>
> my $sql = qq { INSERT INTO
> product
> ( sku, partNum, name, descr, stockNum, qty,
> img, vendNum, price )
> VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ? )
> };
>
> my $sth = $DBH->prepare($sql);
>
> die "No statement handle: ", $DBH->errstr unless
> $sth;
>
> my $rv = $sth->execute( $sku, $partNum, $name,
> $descr, $stockNum, $qty, $img, $vendNum, $price );
>
> doe "Something was wrong: ", $sth->errstr unless
> defined $rv;
>
> print "JOY";
>
> $DBH->disconnect;
> -------------------------- cut here
> ------------------------
>
>
> This works.
>
> By the way, in copying your program I found your
> bug. Look lin line
> 20 and it should stand out.
>
>
>
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>