Gossamer Forum
Home : General : Perl Programming :

execute failed: Unknown column

Quote Reply
execute failed: Unknown column
I have tried umpteen versions of the script below and always get moroless the same error message can any one tell me which direction I should look for the mistake before the variable it sends in the message or after the end of the code it sends back oris it the actual piece of code shown in the message. Whatever I can not get it to work and would love some help please. The line 17 always comes up and is always $query->finish(); which makes sense when it cannot do it!

[Wed Jan 07 13:05:08 2004] [error] [client 127.0.0.1] Premature end of script headers: Guide_delete.pl
[Wed Jan 07 13:05:08 2004] [error] [client 127.0.0.1] DBD::mysql::st execute failed: Unknown column 'Guide.CD00304' in 'where clause' at C:/Program Files/Apache Group/Apache2/cgi-bin/Guide_delete.pl line 15.
[Wed Jan 07 13:05:08 2004] [error] [client 127.0.0.1] Can't call method "disconnect" on an undefined value at C:/Program Files/Apache Group/Apache2/cgi-bin/Guide_delete.pl line 17.

#!c:\perl\bin\perl.exe
use CGI ":standard";
@fieldname = ("Code","Name","Family");
$table ="Guide";
$Code=param("Code");
$Name=param("Name");
$Family=param("Family");
open (OUTFILE, ">>Guide_delete.txt")or die "could not open Guide_delete.txt for writing\n";
print OUTFILE "$Code $Name $Family \n";
close (OUTFILE);
use DBI;
$DBH= DBI->connect("DBI:mysql:Guide")or die "\n ($DBI::err) : $DBI::errstr\n";
my $query = $DBH ->prepare(qq{delete from Guide where Guide.Code like Guide.$Code
});
$query->execute();
$query->finish();
$DBI->disconnect();

#!c:\perl\bin\perl.exe
use CGI ":standard";
@fieldname = ("Code","Name","Family");
$table ="Guide";
$Code=param("Code");
$Name=param("Name");
$Family=param("Family");
open (OUTFILE, ">>Guide_delete.txt")or die "could not open Guide_delete.txt for writing\n";
print OUTFILE "$Code $Name $Family \n";
close (OUTFILE);
use DBI;
$DBH= DBI->connect("DBI:mysql:Guide")or die "\n ($DBI::err) : $DBI::errstr\n";
my $query = $DBH ->prepare(qq{delete from Guide where Guide.Code like $Code AND Guide.Name like $Name AND Guide.Family like $Family
});
$query->execute();
$query->finish();
$DBI->disconnect();
Quote Reply
Re: [Silver Machine] execute failed: Unknown column In reply to
1) Are you checking the value of $Code? Be really careful with sending form values like that to a database. What if someone entered something like this in the form field: whatever; DROP DATABASE Code

Your query would be: delete from Guide where Guide.Code like Guide.whatever; DROP DATABASE Code. As long as the first query is valid and executes, the second one will too. And it won't be pretty :)

Check the value of $Code and make sure you're actually generating a valid SQL statement.

Also check the DBI pod for info on placeholders and quoting.

http://search.cpan.org/...ders_and_Bind_Values
http://search.cpan.org/...BI-1.39/DBI.pm#quote

2) $DBI->disconnect(); should be $DBH->disconnect();.


Hopefully that'll help a little.

~Charlie
Quote Reply
Re: [Chaz] execute failed: Unknown column In reply to
Thanks, it seems that my main problem was the DBH once I sorted that the rest helped its self, the $ needed to be replaced with ? and you are quite right about the whatever delete but I have used other constraints. Thank you it is always something so simple and obvious to everyone else isnt it? I get word blind.! Chris
Quote Reply
Re: [Silver Machine] execute failed: Unknown column In reply to
This is line 17:

$DBH->execute($query)
I think or the one above as it breaks on to a second line. Either way I do not understand what is going on. Help please any one or Chaz if you are around. My DB is called guide and I copied the first 15 lines form a working script. Any ideas out there I am just not able enough.Chris

[Thu Jan 08 18:29:20 2004] [error] [client 127.0.0.1] Premature end of script headers: guide_edit.pl
[Thu Jan 08 18:29:20 2004] [error] [client 127.0.0.1] Can't locate object method "execute" via package "DBI::db" (perhaps you forgot to load "DBI::db"?) at C:/Program Files/Apache Group/Apache2/cgi-bin/guide_edit.pl line 17.


#!c:\perl\bin\perl.exe
use CGI ":standard";
$Code=param("Code");
$Name=param("Name");
$Family=param("Family");
$Sex=param("Sex");
$Maturity=param("Maturity");
$Age=param("Age");
$Length=param("Length");
$Notes=param("Notes");
open (OUTFILE, ">>guide_edit.txt");
print OUTFILE "$Code $Name $Family $Sex $Maturity $Age $Length $Notes\n";
close (OUTFILE);
use DBI;
$DBH=DBI->connect("DBI:mysql:guide")or die "\n ($DBI::err) : $DBI::errstr\n";
$query=$DBH ->prepare(qq{UPDATE guide SET WHERE Code IN(Select Code Name Family FROM guide WHERE guide.Code like ? AND

guide.Name like ? AND guide.Family like ?)});
$DBH->execute($query)
or die "\n ($DBI::err): $DBI::errstr\n";
$query->finish();
$DBH->disconnect();