I have a function that writes a record to a database, by calling two functions: The first creates a hash of data to be inserted and the second assembles an SQL statement out of the hash. then the SQL statement is executed via $sth->do

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:

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 parameters are hebrew, regular win-1255 encoding.

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.


In reply to Unicode Problems with DBI? by yosefm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.