yosefm has asked for the wisdom of the Perl Monks concerning the following question:
Their code is:
sub writeElems { my ($tbl, $prm, $dbh) = (shift, shift, shift); #table name, CGI object, DBI connection. my %valid = preinsert($prm, $tbl); #Creates the hash if (%valid) { $dbh->do(insertstr($tbl, %valid)); } #Creates and ex +ecs the statement return %valid; }
This one gets the parameters:
# A sub to take CGI parameters, untaint and validate them. # Returns: a hash with ready-to-insert data, # or undef if any field fails to validate or untaint; # Arguments: A CGI object, the name of a table to prepare. sub preinsert { my $page = shift; my $tbl = shift; my (%fields, %retval); foreach ($page->param) { if ($_ =~ /^$tbl\./) { s/^($tbl\.)//; $fields{$_} = $page->param("$tbl.$_"); } } #There's a table XML descriptor, and it's just fine. my $dsc = XMLin(M_LIB."/$tbl.descriptor", ForceArray => ['field']); foreach my $fieldref (keys %{$dsc->{field}}) { my %tags = %{$dsc->{field}->{$fieldref}}; my $untaint; return undef if ($fields{$fieldref} !~ /$tags{untaint}/); $untaint = $1; print STDERR "recieved $1\n"; if ($untaint =~ /$tags{validate}/) { print STDERR "transmitted", ($retval{$fieldref} = $untaint), " +\n"; } else { print STDERR "Invalid data"; return undef; } } return %retval; }
And the statement is constructed with:
# Prepares the insert statement string, using results of preinsert. sub insertstr { my $tbl = shift; my %fields = @_; my $str = "insert into $tbl set"; foreach (keys %fields) {print STDERR "$fields{$_}\n"; $str .= " $_=\'$fields{$_}\',";} #watch this print: chop($str); print STDERR "$str\n"; return $str.";"; }
No problem, right? Well, when used in english, there's no problem.
now, I call it twice, like this:
The parameters are hebrew, regular win-1255 encoding.if (scalar($page->param) > 1) { $Xtable = getXTableName($mtype, $dbh); $page->param('items.media_type', $mtype); #This is it: writeElems('items', $page, $dbh); writeElems($Xtable, $page, $dbh) if $Xtable; $page->delete('items.media_type'); }
The first call goes perfect.
Now the wierd stuff: The second call gives garbage, only after the concatenation in &insertstr and it must be unicode, cause it's exactly twice the number of chars and I've been there before...
Why only the second time around? Why me? What did I do wrong? Can't figure that out.
Thanks for your help.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unicode Problems with DBI?
by chromatic (Archbishop) on Jul 01, 2003 at 19:25 UTC | |
|
Re: Unicode Problems with DBI?
by graff (Chancellor) on Jul 02, 2003 at 03:03 UTC | |
by yosefm (Friar) on Jul 02, 2003 at 10:56 UTC | |
|
Re: Unicode Problems with DBI?
by yosefm (Friar) on Jul 02, 2003 at 10:48 UTC |