Mailing List Archive

Having problems passing form variables
I realize this may be a silly question but I cannot get this to work. I'm
trying to pass form variables into a database using DBD::DB2..

$sth = $dbh -> prepare ("INSERT INTO USERDATA(FIRST_NAME)
VALUES($fdat{cgifirst})") or die "Cannot INSERT to '$table'" ;


Is there a different way I need to reference my variables?

Any help is much appreciated.

--Diana
Re: Having problems passing form variables [ In reply to ]
It would be nice to see your embperl.log file
(probably automatically written to /tmp/embperl.log)

Also, what error messages are you getting?

Finally, use Data::Dumper and print out the form data
that you think exists like this:

[+ use Data::Dumper;

Data::Dumper->Dump([\%fdat],['fdat']);
+]

then you will know what is passed.

And of course, instead of using DBI, you might take
alook at Gerald's DBIx::Recordset, his companion
product to HTML::Embperl. You can read more about it
about http://perl/apache.org/embperl

--- "Henrickson, Diana" <DHenrickson@vignette.com>
wrote:
> I realize this may be a silly question but I cannot
> get this to work. I'm
> trying to pass form variables into a database using
> DBD::DB2..
>
> $sth = $dbh -> prepare ("INSERT INTO
> USERDATA(FIRST_NAME)
> VALUES($fdat{cgifirst})") or die "Cannot INSERT to
> '$table'" ;
>
>
> Is there a different way I need to reference my
> variables?
>
> Any help is much appreciated.
>
> --Diana
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> embperl-unsubscribe@perl.apache.org
> For additional commands, e-mail:
> embperl-help@perl.apache.org
>


=====
Terrence Brannon
90 St. Mark's Place
Apt. 2E North
Staten Island, NY 10301
914-755-4360

__________________________________________________
Do You Yahoo!?
Yahoo! Photos -- now, 100 FREE prints!
http://photos.yahoo.com
Re: Having problems passing form variables [ In reply to ]
"Henrickson, Diana" wrote:

> I realize this may be a silly question but I cannot get this to work. I'm
> trying to pass form variables into a database using DBD::DB2..
>
> $sth = $dbh -> prepare ("INSERT INTO USERDATA(FIRST_NAME)
> VALUES($fdat{cgifirst})") or die "Cannot INSERT to '$table'" ;
>
> Is there a different way I need to reference my variables?
>

Occasionally I find I need to copy hash values to temp variables to get
things to work right.

my $cgifirst = $fdat{'cgifirst'};
$sth = $dbh->prepare ("INSERT INTO $table (FIRST_NAME)
VALUES('$cgifirst')"or die "Cannot INSERT to '$table'" ;

Otherwise I get some cryptic hash reference instead of the value. I
remember stumbling across an explanation of this somewhere. It had to
do with the evaluation of hash references in a string context. Of
course I can't find that information back right now. I don't believe
the problem has anything to do with Embperl or DBI though. It is
strictly a Perl thing I think.

Sorry I can't be more helpful.
Bryan.

--
Bryan Thale
Motorola Labs, Networking and Infrastructure Research
mailto:thale@rsch.comm.mot.com
Re: Having problems passing form variables [ In reply to ]
From: "Henrickson, Diana" <DHenrickson@vignette.com>

I realize this may be a silly question but I cannot get this to work. I'm
trying to pass form variables into a database using DBD::DB2..

$sth = $dbh -> prepare ("INSERT INTO USERDATA(FIRST_NAME)
VALUES($fdat{cgifirst})") or die "Cannot INSERT to '$table'" ;


Is there a different way I need to reference my variables?

Any help is much appreciated.

--Diana

You didn't give us much info to go on, but there are only a view
possibilities here.

The first is that $fdat{cgifirst} doesn't contain a value. Another
list member has already addressed that.

The most likely problem is that you are getting a database error,
probably a syntax error. The reason is that cgifirst is a string and
probably needs to be enclosed in quotes in the SQL. The variables
that need to be in quotes verses number that are not in quotes and all
the rest cause lots of problem when you substitute strings into your
SQL as your example shows you doing. The "better" way is to do the
following: (Apologies for any syntax errors, I haven't actually run
the following)

$sth = $dbh -> prepare ("INSERT INTO USERDATA(FIRST_NAME)
VALUES(?)") or die "Prepare failed: " .$dbh->errstr;
$rv = $sth->execute($fdat{cgifirst} or die "Insert failed: " .$dbh->errstr;

This technique uses the ? as a place holder for the value which is
passed in the execute call. This eliminates the quoting problem.

The DBI documentation has some examples of using placeholders so you
should start there and you could get the new O'Reilly book on DBI and
review it for the list.

Dudley
RE: Having problems passing form variables [ In reply to ]
>
> Occasionally I find I need to copy hash values to temp variables to get
> things to work right.
> [..]
>
> Otherwise I get some cryptic hash reference instead of the value. I
> remember stumbling across an explanation of this somewhere.

You must not insert spaces between a variable inside a double quoted string,
i.e.:

"$fdat{foo}" -> ok
"$fdat {foo}" -> doesn't work

Gerald

-------------------------------------------------------------
Gerald Richter ecos electronic communication services gmbh
Internetconnect * Webserver/-design/-datenbanken * Consulting

Post: Tulpenstrasse 5 D-55276 Dienheim b. Mainz
E-Mail: richter@ecos.de Voice: +49 6133 925151
WWW: http://www.ecos.de Fax: +49 6133 925152
-------------------------------------------------------------