I want to use a checkbox form to allow a user to select which products they want to list all items for. The form will post or get to a .pl script which connects to a mysql database to extract the list and display the output in an html page. So far ive got:
form.html:
CODE
<form action="/cgi-bin/listproducts.pl" method = "GET">
<input type = "checkbox" name="list" value = 1>Desktops<br>
<input type = "checkbox" name="list" value = 1>Hard Drives<br>
<input type = "checkbox" name="list" value = 1>Laptops<br>
<input type = "submit" value = "Submit">
</form>
listproducts.pl:
CODE
use CGI;
use CGI qw( :standard -debug);
use CGI::Carp qw(fatalsToBrowser);
$userdb="mydatabase";
$dbserveraddy="localhost";
$username="root";
$password="root";
#Create an instance of the CGI object
$cgiobject = new CGI;
#Grab values submitted by the user
my @productList=$cgiobject ->param("list");
#for each group of products checked, select all values from database
foreach $product (@productList){
use DBI;
use DBD::mysql;
$dbh=DBI->connect("dbi:mysql:$userdb:$dbserveraddy","$username","$password")
or die "Connection error: $DBI::errstr\n";
#Define MYSQL query
$sth=$dbh->prepare("SELECT * FROM '$product'");
$sth->execute ||
die "Could not execute SQL statement... maybe invalid syntax?";
#fetch each row and print results
while (@results = $sth->fetchrow_array())
{
print STDOUT "$results\n";
}
$sth->finish;
$dbh->disconnect;
}
#Output HTML header to the Web Browser
print $cgiobject -> header;
But this is returning nothing to the page. When I check a box, say the Desktops box and hit Submit, I get a blank screen back, where the URL reads: "http://localhost:8888/cgi-bin/listproducts.pl?Desktop=1"
Im not sure where the problem lies - am I on the wrong lines trying to use a mysql query inside a foreach loop? Is my syntax correct to take each '$product' scalar from the @productList array and use it in my 'SELECT * FROM...' query?
Additionally, I need the results returned to have a checkbox next to each item so the user can delete it from the database. Would be very grateful if someone could point me in the right direction, or if they know any good tutorials that are closely linked to this sort of thing.
Thanks in advance.
EDIT: rename title
This post has been edited by b_real: 12 Apr, 2008 - 11:18 AM