#! /usr/local/bin/perl # Order Form Generation Lab: Solution with Extra for Experts # # Begin loading the needed extensions # use DBI; # The DBI extension use CGI qw (:html2 :form); # The CGI extension use CGI::Carp qw (carpout); # The extra carping routine # # The file dbtype.pl sets a variable which determines the type of database # you will use. This file will be found in either /tmp or /temp. # use lib qw(/tmp /temp); require 'dbtype.pl'; # # Set the path, unbuffer STDOUT, and redirect errors to the browser # $ENV{PATH} = join ":", qw(/usr/bin /bin /sbin /usr/sbin /etc); $| = 1; carpout(\*STDOUT); # # Declare variables # my( $dbh, $rv, $sth ); my( $tid, $name, $price, $edition ); my( @sequence, %labels ); # # Accept the CGI request # my( $q ) = new CGI; # # Send the HTTP header # print $q->header; # # Set the and send <BODY> # print start_html("Nile dot Com Order Form"); # # Attempt to load the driver and connect to the database # eval { $dbh = DBI->connect("DBI:$DB_type:test_nile", $DB_login, $DB_password); }; # # Check if the eval failed (driver failed to load) # if ($@) { die "Could not install driver ($DBI::err): $DBI::errstr\n"; } # # Check if the connection attempt was successful # defined($dbh) or die "Could not connect to db ($DBI::err): $DBI::errstr\n"; # # Prepare the following SQL statement and receive a statement handle # $sth = $dbh->prepare(q{ SELECT title_id, name, price, edition FROM title }) or die "Prepare of SELECT: ", $dbh->errstr, "\n"; # # Set up the column bindings to match the column names requested # in the SELECT statement # $sth->bind_columns(undef, (\$tid, \$name, \$price, \$edition)) or die "Binding of columns: ", $dbh->errstr, "\n"; # # Execute the prepared SQL statement # $sth->execute or die "Execution of SELECT: ", $dbh->errstr, "\n"; # # Loop over every row returned by the SELECT statement # Notice that due to the bindings done earlier, the variables will # automatically be loaded by the fetch method # while ($sth->fetch) { push(@sequence, $tid); $labels{$tid} = "$name ($price)"; } # # We're done with this statement, free up the resources # $sth->finish; # # Disconnect from the database, we're done! # $dbh->disconnect or warn "Disconnect: ", $dbh->errstr, "\n"; # Sort the titles in alphabetical order @sequence = sort { $labels{$a} cmp $labels{$b} } @sequence; # # Print out a nice big header, # print h1("Order Form"); print startform( -action => '/cgi-bin/dbweb-cgi/purchase.cgi' ); print "<table><tr>\n"; print "<td>Name: "; print "<td>", textfield( -name => "cust_name", -size => 40, -maxlength => 80, ); print "<tr>"; print "<td>Address: "; print "<td>", textfield( -name => "cust_address", -size => 40, -maxlength => 80, ); print "<tr>"; print "<td>Credit Card: "; print "<td>", radio_group( -name => 'card_type', '-values' => ['Visa', 'MasterCard', 'Discover', 'Other'], -linebreak => 'true', ); print "<tr>"; print "<td>Card Number: "; print "<td>", textfield( -name => "card_number", -size => 20, -maxlength => 30, ); print "<tr>"; print "<td>Copies: "; print textfield( -name => "num_copies", -size => 2, -maxlength => 3, ); print "<td>"; print scrolling_list( -name => 'title_list', '-values' => \@sequence, -size => 10, -labels => \%labels); print "</table>"; print p; print submit( -name => 'Order Now!' ); print reset( -name => 'Clear' ); print endform; print "</table>"; print p; # # Send </BODY></HTML> # print end_html; exit 0;