collect all numeric form params and update database
Anonymous Monk
created: 2006-03-05 20:13:41
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.

Re: collect all numeric form params and update database
created: 2006-03-05 20:20:49
Here you go:
# assuming $cgi = new CGI;

# prepare update query
my $sth = $dbh->prepare(' update pic_table set pic_value=? where pic_id=? ');

# 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 );
}
Re^2: collect all numeric form params and update database
created: 2006-03-05 20:32:53
That's actually very neat, however I am not using OOP on this. How would this be done without using CGI as an object?
Re^3: collect all numeric form params and update database
created: 2006-03-05 20:34:56
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 :)

perlmonks.org content © perlmonks.org and Anonymous Monk, rhesa

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03