Welcome to Dream.In.Code
Getting Help is Easy!

Join 86,262 Programmers. There are 1,902 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

checkbox to list all products

 
Reply to this topicStart new topic

checkbox to list all products

b_real
post 12 Apr, 2008 - 11:17 AM
Post #1


New D.I.C Head

*
Joined: 29 Dec, 2006
Posts: 12



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
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


b_real
post 12 Apr, 2008 - 01:22 PM
Post #2


New D.I.C Head

*
Joined: 29 Dec, 2006
Posts: 12

QUOTE
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"


OK, ive done a bit of debugging, so this is no longer true. I figured out that I had to change the value of each checkbox from '1' to the name of the table I want to select from. The URL now reads something like: "http://localhost:8888/cgi-bin/listproducts.pl?list=Desktop", so now my .pl script actually searches the right table.

However, the .pl script now returns another message caught by the $sth->execute || die "Could not ... " statement:

CODE

listproducts.pl: DBD::mysql::st execute failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Desktop'' at line 1 at /Applications/MAMP/cgi-bin/listproducts.pl line 32., referer: http://localhost:8888/perl/form.html
[Sat Apr 12 21:08:30 2008] [error] [client 127.0.0.1] [Sat Apr 12 21:08:30 2008] listproducts.pl: Could not execute SQL statement... maybe invalid syntax? at /Applications/MAMP/cgi-bin/listproducts.pl line 32., referer: http://localhost:8888/perl/maintenance.html


So looks like something wrong with the SQL syntax... but what?

This post has been edited by b_real: 12 Apr, 2008 - 01:27 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 10:20AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month