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

I have a form that has form fields populated from a database.

It's just text fields with a numeric name and one submit button.

What I need to do is find a way to store all these numeric params into a hash (as I assume this is the best way?) and then update a mysql every numeric name (which is actually called pic_id in the database) with the value of that parameter.

Anyone have thoughts on how to do this? I could in theory parse the database before I collect the form params (after it was submitted) and get the available pic_ids that I expect and make each their own variable. Then get the form params in their own scalar. But I doubt that's the best way.

  • Comment on collect all numeric form params and update database

Replies are listed 'Best First'.
Re: collect all numeric form params and update database
by rhesa (Vicar) on Mar 06, 2006 at 01:20 UTC
    Here you go:
    # assuming $cgi = new CGI; # prepare update query my $sth = $dbh->prepare(' update pic_table set pic_value=? where pic_i +d=? '); # loop over all param names, and # grep out all those that are numerical foreach my $p( grep /^\d+$/, $cgi->param) { # find the form value my $v = $cgi->param( $p ); # execute the query $sth->execute( $v, $p ); }
      That's actually very neat, however I am not using OOP on this. How would this be done without using CGI as an object?
        In that case, you can simply omit the $cgi->:
        foreach my $p( grep /^\d+$/, param() ) { # find the form value my $v = param( $p ); # execute the query $sth->execute( $v, $p ); }
        Note that DBI still uses OOP :)