Eeek. For one, unless you really, really trust your users to type

state = 'CA'
into the form, you're asking for trouble. Google for "SQL injection attack" or use super search here.

Far better is to do something like

my $sql = "SELECT city FROM table WHERE state = ?"; my $sth = $dbh->prepare($sql); $dbh->execute($FORM{state});
By doing this, the value will be quoted automagically as appropriate for MySQL, elimating the possibility that someone will type
1; drop table database
into the form and have the results ruin your day.

Then you have to read the results before finishing the statement handle and disconnecting.

Also, since you're using CGI, it is completely unecessary to attempt to disect the query string yourself (and dissecting query strings by hand can be trickier than it first appears). CGI is quite good at handling that for you. You can drop that entire chunk of code that sets up %FORM, and instead do

use CGI; my $cgi = CGI->new(); ... my $sth = $dbh->prepare($sql); $sth->execute($cgi->param('state')) or die ...

In reply to Re: Trying to print HTML table, erroring out. by dws
in thread Print Array into 4 Column Table 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.