Hi, I'm hoping that someone can take a look at my script.
I'm using Perl in combination with mySQL, with variables read in from an html form. The problem seems to be that the variables don't get read in properly.
It connects to SQL fine, but then gives the message "there is no matching image for item" (it doesn't print the $item_number variable -- none seems to be present, although I have posted from a form with "hidden" input type names and values).
When I subsitute the literal filename for the variable, it works perfectly.
Here's the script:
#!/usr/bin/perl
use strict;
use lib qw(C:/Perl/lib);
use CGI qw
standard escapeHTML);
use DBI;
use mysql;
my $query;
my $item_name;
my $item_number;
my @pairs;
my $pair;
my $name;
my $value;
read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
%form = &parse;
$item_name = $form{'item_name'};
$item_number = $form{'item_number'};
&deliver_product;
sub parse {
@pairs = split(/&/, $query);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$form{$name} = $value;
}
sub deliver_product {
my $image_dir = "http://www.mysite.com/dir";
my $path_sep = "/";
my $dbh = &connect;
my $finished = $dbh->selectrow_array ("SELECT printfinished FROM images WHERE webfinished = '$item_number'") or die "No matching image for item $item_number";
my $line = $dbh->selectrow_array ("SELECT printline FROM images WHERE webfinished = '$item_number'") or die "No matching image for item $item_number";
my $finished_path = $image_dir . $path_sep . $finished . ".jpg";
my $line_path = $image_dir . $path_sep . $line . ".jpg";
$dbh->disconnect ();
print <<EOF;
Content-Type: text/html
<img src=$finished_path width="300" height="300" border=1></td>
<img src=$line_path width="300" height="300" border=1></td>
}
}
sub connect
{
my $db = "xxxxxxx";
my $host = "xxxxxxxxx";
my $db_user = "xxxxxxxx";
my $db_password = "xxxxxxx";
my $dsn = "dbi:mysql:$db:$host";
return (DBI->connect ($dsn, $db_user, $db_password, {PrintError => 0, RaiseError =>1}));
}
exit (0);
I'm using Perl in combination with mySQL, with variables read in from an html form. The problem seems to be that the variables don't get read in properly.
It connects to SQL fine, but then gives the message "there is no matching image for item" (it doesn't print the $item_number variable -- none seems to be present, although I have posted from a form with "hidden" input type names and values).
When I subsitute the literal filename for the variable, it works perfectly.
Here's the script:
#!/usr/bin/perl
use strict;
use lib qw(C:/Perl/lib);
use CGI qw
use DBI;
use mysql;
my $query;
my $item_name;
my $item_number;
my @pairs;
my $pair;
my $name;
my $value;
read (STDIN, $query, $ENV{'CONTENT_LENGTH'});
%form = &parse;
$item_name = $form{'item_name'};
$item_number = $form{'item_number'};
&deliver_product;
sub parse {
@pairs = split(/&/, $query);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$form{$name} = $value;
}
sub deliver_product {
my $image_dir = "http://www.mysite.com/dir";
my $path_sep = "/";
my $dbh = &connect;
my $finished = $dbh->selectrow_array ("SELECT printfinished FROM images WHERE webfinished = '$item_number'") or die "No matching image for item $item_number";
my $line = $dbh->selectrow_array ("SELECT printline FROM images WHERE webfinished = '$item_number'") or die "No matching image for item $item_number";
my $finished_path = $image_dir . $path_sep . $finished . ".jpg";
my $line_path = $image_dir . $path_sep . $line . ".jpg";
$dbh->disconnect ();
print <<EOF;
Content-Type: text/html
<img src=$finished_path width="300" height="300" border=1></td>
<img src=$line_path width="300" height="300" border=1></td>
}
}
sub connect
{
my $db = "xxxxxxx";
my $host = "xxxxxxxxx";
my $db_user = "xxxxxxxx";
my $db_password = "xxxxxxx";
my $dsn = "dbi:mysql:$db:$host";
return (DBI->connect ($dsn, $db_user, $db_password, {PrintError => 0, RaiseError =>1}));
}
exit (0);