Using a checkbox to retrieve values from a mysql database. So far, my form looks like:
<form action="/cgi-bin/listproducts_test.pl" method = "GET"> <input type = "checkbox" name="list" value = 'book'>Book<br> <input type = "checkbox" ...will have extra checkboxes here<br> <input type = "submit" value = "list"> </form>
key parts of listproducts.pl looks like:
#Create an instance of the CGI object $cgiobject = new CGI; #Output HTML header to the Web Browser print $cgiobject -> header; #Grab values submitted by the user my $product=$cgiobject ->param("list"); $dbh=DBI->connect("dbi:mysql:$userdb:$dbserveraddy","$username","$ +password") or die "Connection error: $DBI::errstr\n"; #Define MYSQL query - i think this is where I need assistance $sql = "Select * FROM ?"; $sth=$dbh->prepare($sql); $sth->execute($product) || + #ERROR HERE die "Could not execute SQL statement... maybe invalid syntax?"; print "<table border=2>"; #fetch each row and print results - also need assistance with this + if its wrong while (@results = $sth->fetchrow_array()) { foreach $result (@results){ if ($result eq $results) { print "<tr><td>$results</td></tr>"; } } } print "</table>"; $sth->finish; $dbh->disconnect;
The die catch is telling my my SQL syntax is invalid, but I cant see why. This is a basic version of the syntax to highlight the SQL problem (points to the line indicated by #ERROR HERE). I plan to have additional values in the checkbox and use a foreach loop around the $dbh and $sth handles. Basically, ive got 3 tables of products and I want to be able to list all products in each table if checked in the checkbox. Am I on the right lines?

In reply to checkbox and mysql select by breal

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.