Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

inserting all form fields into db

by emilford (Friar)
on Apr 20, 2005 at 20:46 UTC ( [id://449765]=perlquestion: print w/replies, xml ) Need Help??

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

I found this node as a start, but would like to simplify the process of matching up all fields and field values more dynamically. I have a form that will survey visitors of about 50 questions. Each form field has a name that corresponds to a column in our database. In jeffa's approach, he created an array of fields and a corresponding array of field values which is used to generate the INSERT statement. Great start, but is there a better method. If I used CGI's param() to obtain a list of all fields submitted, could I go from there? 2 problems, I'd probably get a series of additional fields (hiddens, submits, etc)? and also am unsure how to deal with multi-valued params. Any suggestions?

Replies are listed 'Best First'.
Re: inserting all form fields into db
by johnnywang (Priest) on Apr 20, 2005 at 20:57 UTC
    Maybe you can look at Class::DBI, once you get your parameters as a hash, you can simply call the "create" method, passing a reference to the hash. It will insert for you, you don't even need to construct the sql.
Re: inserting all form fields into db
by jZed (Prior) on Apr 22, 2005 at 03:56 UTC

    You really should do inserts based on the fields as they exist in the database, not the fields submitted by the form. Either hardcode the field names for the table, or to do it dynamically, use $sth->{NAME} to get a list of the field names for each table. If you use CGI's Var method, you can have a hash of the params and only insert if the key to the hash matches a table field name. You may run into issues of case so it's safest to turn everything to lower or upper with $sth->{NAME_lc} or $sth->{NAME_uc} and always name your form fields with that case.

    I can't answer your question about how to handle multi-valued params without knowing the structure of your datbase table. Are the multiple values to be stored in multiple fields? They probably should be to keep your tables normalized, but it's hard to say without knowing what you are trying to do. If you really must have multiple values in a single field, then use join() or some other method of serializing the multiple fields into a single string.

    If you aren't using DBI placeholders, I really, really recommend that you read up about them before implenting your script, you will need placeholders for CGI form values.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://449765]
Approved by polettix
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found