I'm assuming that you already know how to connect to your database and execute an SQL statement with Perl. I'm also assuming that you know how to create the HTML form.

Let's say you have a form with one option being a numeric user ID with a name of 'id' and the other option a color named 'color' that you want to set for that user ID (let's say you're setting a color preference). The following (untested) example assumes that the table is called 'preferences' and that the appropriate field names are 'id' and 'color'.

#!/usr/bin/perl -wT use strict; use CGI; use DBI; # I'm just assuming that you use the DBI module for # database connectivity. Substitute another module # as appropriate. my $query = new CGI; # The regexes are for "taint checking" $query->param('color') =~/^([a-zA-Z]+)$/ or die "Bad data in color"; my $color = $1; $query->param('id') =~/^([0-9]+)$/ or die "Bad data in id"; my $id = $1; my $database = 'preferences'; my $sql = "UPDATE $database SET color='$color' WHERE id='$id'"; # now, connect to the database and execute the SQL.
Note, this is just an example and is NOT the most efficient way of constructing the SQL statement, but it's simple and effective. Further, it assumes that the record already exists. If it doesn't you'd have to use an INSERT statement.

Regarding the taint checking, see perlsec for details. If you are unfamiliar with connecting to a database with Perl, please read A Short Guide to DBI.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just go the the link and check out our stats.


In reply to (Ovid) Re: saving form values to database by Ovid
in thread saving form values to database by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.