> 
> Hi all,
>  
> I need to present a series of database fields for users to update and when
> the form is submitted I need to be able to retrieve not only the field
> values but the original row ID's as well so I'll know which table rows to
> update. 
>  
> What I'd like to be able do is name the form fields as array elements:
>  
>      <form>
>      <input type="text" name="question{'25'}" value="$answer{'25'}">
>      <input type="text" name="question{'47'}" value="$answer{'47'}">
>      </form>
>  
> Then when the form came back in, create update statements from the
key/value
> pairs.
>  
>      while (($key,$value) = each %question) {
>           $query = "UPDATE tablename SET answer = '$value' WHERE id =
'$key'
> ";
>      } 
>  
> Does that make any sense at all? Can someone tell me how I'd do that in
> PERL?
>  

It makes sense and your solution is close. Rather than using difficult
names in your input tags just use simplified names, as just submitting
the form won't make your %question spring into life automagically.  So
given something like:

<input type=text name="question_45" value="$answer{'45'}">
<input type=text name="question_27" value="$answer{'27'}">

Then the answers get interpolated in and/or updated by the client end.
Then when they submit you will look for something like:

my %params = $req->params;

while (my ($key, $val) = each (%params)) {
  if ($key =~ /^question_(\d+)$/) {
      my $question = $1;
      my $st = "UPDATE tablename SET answer='$val' WHERE id=$question";
       ...
  }
}

This steps through the list of all input parameters, as the names should
be unique, looking for a particular criteria, in this case the string
'question_' followed by a set of digits that we know correspond to the
ids of the questions. By using the (\d+) we capture that idea into $1,
which I then set to $question to make it easy to read. The value
associated with that key is your answer.  I tried to make this verbose,
there is shorter typing that would get the job done and I would suggest
using answer=? with a binding value to avoid single quote snafu's, etc.
but you get the general idea.

It's 'Perl' or 'perl' depending on whether you are talking about the
language or the interpreter, but not PERL....

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to