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

Can someone tell me why this script will not work when called by another script.
It works when I set the variables inside the script.
this is what and how the parms are passed.
UnSignup.pl?name=Kari+Borer&remove=2002-04-14&rows=Submit&.cgifields=remove
#!/usr/bin/perl -w #Volunteer.pl - Delete dates from Volunteer use strict; use warnings; use diagnostics; use DBI; use CGI; my $query = CGI::new(); my ($dbh, $sth, $sql, $date, $col); my @remove_dates = $query->param("remove"); my $name = $query->param("name"); #my @remove_dates = ("2002-04-21,"); #my $name = "Kari Borer"; $dbh = DBI->connect ("DBI:mysql:host=localhost;database=mnlight_MNLIGH +TNING", "mnlight","password", {PrintError=> 0, RaiseError=> 1}); print $query->start_html; # undo the the dates signed up this session. foreach $date ( @remove_dates ) { $sql = "update Volunteer set Volunteer = 'TBD' WHERE Date = '$ +date' and Volunteer = '$name'"; $sth = $dbh->prepare($sql) || die "prepare: $$sql: $DBI::errst +r"; $sth->execute || die "execute: $sql->[0]: $DBI::errstr"; print $query->h1({-align=>'center'}, "You are no longer signed + up for this date $date"); } #end foreach loop $sth->finish (); $dbh->disconnect (); print $query->h2({-align=>'center'}, $query->a({href=>'../BobSchedule.pl'}, 'BOBS SCHEDULE'), + '&nbsp',); $query->end_html; exit(0)

Replies are listed 'Best First'.
Re: Failing when called by another script
by seattlejohn (Deacon) on Mar 08, 2002 at 02:58 UTC
    No specific answer, but you might try adding print statements to confirm that your variables are getting set with the values you expect, that your SQL statement looks right, etc...

    One thing that seems odd is the comma after the date in this line:#my @remove_dates  = ("2002-04-21,"); (I realized it's commented out, just not sure why it would be there in the first place.)

Re: Failing when called by another script
by rdfield (Priest) on Mar 08, 2002 at 10:22 UTC
    Without seeing the actual error message generated, only the most general advice can be given:
  • Use placeholders instead of interpolated values (security and performance)
  • The 'die' message after both the parse and the execute uses $sql as a reference, when it's clearly initialised as a string.
  • I think that @remove_dates should be initialised as my @remove_dates = split /\0/,$query->param{remove}; since CGI.pm returns a single string, not an array.

    rdfield

      I think that @remove_dates should be initialised as my @remove_dates = split /\0/,$query->param{remove}; since CGI.pm returns a single string, not an array.

      I don't think that's actually what happens. If you pass a multivalued paramater, (such as a checkbox_group), CGI.pm will return an array.
      From perldoc CGI.pm:
      FETCHING THE VALUE OR VALUES OF A SINGLE NAMED PARAMETER: @values = $query->param('foo'); -or- $value = $query->param('foo'); Pass the param() method a single argument to fetch the value of +the named parameter. If the parameter is multivalued (e.g. from mult +iple selections in a scrolling list), you can ask to receive an array +. Otherwise the method will return a single value.

      Regards,
      davis
      Is this going out live?
      No, Homer, very few cartoons are broadcast live - it's a terrible strain on the animator's wrist