#! /usr/local/bin/perl # # Begin loading the needed extensions # use DBI; # The DBI extension use CGI qw (:html2); # 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, $sth ); my( $tid, $name, $price, $edition ); my( $bookinfo, @bookinfo); # # Accept the CGI request # my( $q ) = new CGI; # # Read values from the form which called us # $tid = $q->param("title_id") || ""; # # Send the HTTP header # print $q->header; # # Set the and send <BODY> # print start_html("Connecting Databases to the Web - Chapter 5, E4E"); # # Verify the validity of the values from the form # $tid > 0 or later("Title Id must be a number greater than zero!"); # Connect to the database or give error messages # # 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(qq{ SELECT * FROM title WHERE title_id = $tid }) or die "Prepare of SELECT: ", $dbh->errstr, "\n"; # # Execute the prepared SQL statement # $sth->execute or die "Execute of SELECT: ", $dbh->errstr, "\n"; # # Grab the values from the row returned by the SELECT statement # (There had better only be one!) # ( $tid, $name, $price, $edition ) = $sth->fetchrow_array; # # 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"; # Build the body of the HTML page print <<END_OF_HTML; <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0> <TR> <TD WIDTH=100> <BR> </TD> <TD WIDTH=450> <CENTER> Chapter 5<P> <H1>Extra for Experts:<BR>Update a Record</H1><P> <CENTER> <FORM METHOD="post" ACTION="/cgi-bin/dbweb-cgi/labupdate_extra2.cgi"> Title ID: $tid<P> <INPUT TYPE="hidden" NAME="tid" VALUE="$tid"> Name of Book: $name<BR> Price of Book<BR> <INPUT TYPE="text" NAME="price" VALUE="$price"><P> Edition Number: $edition<BR> <INPUT TYPE="submit" VALUE="Update the Record"> <INPUT TYPE="reset" VALUE="Reset the Form"> </FORM> </CENTER> </TD> </TR> </TABLE> END_OF_HTML # # Send </BODY></HTML> # print end_html; exit 0; # # This subroutine is used to cleanly exit the program, # sending proper HTML back to the browser # sub later { my( $goof ) = @_; # # Print out a nice big header, # print h1("Could not find that title id."); print "$goof"; # # Send </BODY></HTML> # print end_html; exit 0; }