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

I have the following mysql tables

Products table ============== id name price ...etc images table ============== img_id prod_id img_ext

So when adding products I can add as many images as I want (for now 3 only), but the idea is to leave the number dynamic.

The Problem

The problem is when editing the product I want to be able to replace already uploaded images OR upload some other new images

the idea I had was using the img_id inside the field names like this

<input type="file" name="img[$img_id]">

So if I have an img with an id I'll update the image record otherwise, it's a new image so I'll insert a new record.

I cannot seem to get CGI module to fetch this html input as key,value pairs.

Any idea or new logic would be appreciated

Replies are listed 'Best First'.
Re: fetch html input as hash
by merlyn (Sage) on Dec 25, 2009 at 14:30 UTC
    Do you mean CGI.pm? If so, you can just iterate over them:
    use CGI qw(param upload); # and whatever else you want to import ... for my $key (param()) { next unless $key =~ m{^img\[(.*)\]$}; my $image_key = $1; my $image_filename = param($key); my $image_handle = upload($key); # do what you want with $image_key and $image_filename and $image_ha +ndle }

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Re: fetch html input as hash
by Anonymous Monk on Dec 25, 2009 at 14:34 UTC
    I cannot seem to get CGI module to fetch this html input as key,value pairs.

    It would not come as such, check the file upload section of CGI manual.