Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

regular expression trouble

by *alexandre* (Beadle)
on May 31, 2013 at 13:50 UTC ( #1036248=perlquestion: print w/replies, xml ) Need Help??

*alexandre* has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Whithin the following code I'm getting into trouble
$db->sqlInsert("commentaire", ref_emetteur => "1", ref_article => "$article", question => "$question", text => "$text", date => "$date $time");
package db
sub sqlInsert { my $table = shift || ''; my(%data)=@_; my($names,$values); $dbh||=sqlConnect(); foreach (keys %data) { if (/^-/) { $values.="\n ".$data{$_}.","; s/^-//; } else { $values.="\n ".$dbh->quote($data{$_}).","; } $names.="$_,"; } chop($names); chop($values); my $sql="INSERT INTO $table ($values) VALUES ($names)\n"; #$sql = $dbh->quote ($sql); print "Content-type: text/html\n\n"; print "$sql <br />"; if(!$dbh->do($sql)) { my $err=$dbh->errstr; } }
when I print the resultat of the query on the browser it's give me the following sql
INSERT INTO MyDB=HASH(0xa22d970) ( 'text', 'question', 'ref_emetteur', + 'date', NULL) VALUES (,1,commentaire, this is the description ,2013-05-31 15:16:29)
it's like the commentaire field appear in the values list and the ref_article doesn't be included but a NULL field is defined any idea ? thxx In fact after doing some test on the hash, It's appear that everytime a key / value disapear
$db->sqlInsert("commentaire", ref_article => "$article", question => "$question", text => "$text", ref_emetteur => "1", date => "$date $time"); sub sqlInsert { my $table = shift || ''; my(%data)=@_; print "Content-type: text/html\n\n"; while ( my ($key, $value) = each(%data) ) { print "$value<br/>"; } }
print out
text date ref_article ref_emetteur

Replies are listed 'Best First'.
Re: regular expression trouble
by choroba (Archbishop) on May 31, 2013 at 14:01 UTC
    You should study how objects and methods work in Perl. The sub should start with
    my $self = shift;

    The table is the second parameter, the first one is the invocant.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      In addition to that, this:

      my $sql="INSERT INTO $table ($values) VALUES ($names)\n";

      should probably be

      my $sql="INSERT INTO $table ($names) VALUES ($values)\n";
Re: regular expression trouble
by NetWallah (Canon) on May 31, 2013 at 14:39 UTC
    In addition to this, for the case when the key starts with "-" ( if (/^-/) ),
    You are not appending to $names.

    The "for" loop and "chop"s could be replaced by:

    my $names = join ",\n", map {s/^-//; $_ } keys %data; my $values= join ",\n", map { m/$-/ ? $data{$_} : $dbh->quote($data{$_ +} } keys %data;
    (I have made some assumptions on what you want done when the key starts with "-".)

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1036248]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2022-10-07 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (30 votes). Check out past polls.

    Notices?