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

I am using CGI to accept some input to my script -
if ($cgi->param('Students') eq 'Add') { print "ADDING<br>\n"; for ($i = 1; $i < 6; $i++) { $paramname = "Name" . $i; print $paramname . " = " . $cgi->param($paramname) . "<br>\n"; if ($cgi->param($paramname) != "") { print "GSFDSFDS"; $insertvalue = $cgi->param($paramname); print $insertvalue; #$dbh->do("insert into Students (Name) values ($insertvalu +e)"); #$dbh->disconnect; } }
The 'if ($cgi->param($paramname) != "")' never gets executed even though just prior to it $cgi-param($paramname) is printed successfully. I am using CGI input elsewhere in the script and it works fine. I really can't see what the problem is here and I would really appreciate some help.
Thanks
J.Britton

20040220 Edit by BazB: Changed title from 'Accepting CGI Input'

Replies are listed 'Best First'.
Re: Which comparative operator should I use?
by Skeeve (Parson) on Feb 19, 2004 at 12:34 UTC
    != is for numbers
    ne for strings
Re: Which comparative operator should I use?
by Abigail-II (Bishop) on Feb 19, 2004 at 12:38 UTC
    Of course the if ($cgi->param($paramname) != "") gets executed. It just has a different result than you expect. != is a numerical comparison operator, and hence, turns both it operands into numbers. "" becomes 0. $cgi->param($paramname) is very likely to be a string not starting with a number, and will hence become 0 as well. Which results in 0 != 0, which is false.

    If you turn on warnings, Perl will warn you that this is happening.

    Abigail