
Kanji
User
/ Moderator
Jul 14, 2000, 4:39 PM
Post #3 of 8
(1329 views)
|
Per my response to your email a very simplistic database read/writer ... <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> #!/usr/bin/perl -w ## --/ SCRIPT CONFIGURATION /------------------------------- ## ## ## $dsn is the data source name we need to access the db, ## and takes the form of dbi:DRIVER:DATABASE where DRIVER ## is derived from the DBD::DRIVER modules and DATABASE ## is the name (and host if nedded) of the db we want. ## ## Sample DSNs are dbi:CSV:f_dir=/tmp/my_books (a CSV db), ## dbi:mysql:database=my_books;host=10.0.0.1 (a MySQL db ## hosted a different server), and ## dbi:Oracle:my_books (Oracle). my $dsn = 'dbi:CSV:f_dir=/tmp/mybooks'; ## Username and password needed to authenticate (if any). my $user = ""; my $pass = ""; ## Any extra parameters to pass to control various script ## <-> db behaviours. See the DBI and DBD::DRIVER man ## pages for all possibilities, but we're going with ## automatically committing all our queries, and handle ## errors ourselves. my %attr = ( AutoCommit => 1, PrintError => 0, RaiseError => 0 ); ## --/ MODULES WE NEED /------------------------------------ ## use CGI; # ... for forms processing use CGI::Carp 'fatalsToBrowser'; # ... errors to the browser use DBI; # ... for database interaction use strict; # ... cause we're prim & proper ## --/ DA PROGRAM! /---------------------------------------- ## # Unbuffer out output so that genuine output has a chance to # reach the browser before errors (if any) do. $|++; # Create a CGI object. I generally don't go with the OO # method of CGI.pm, but in this case I think'll it help # distinguish what module is handling what. ;^) my $cgi = CGI->new; # Connect to the database ... my $db = DBI->connect( $dsn, $user, $pass, \%attr ) or die( "Can't connect because ", DBI->errstr ); # Output HTTP headers, and the HTML <head>'er. print $cgi->header, $cgi->start_html( -title => 'form2db example' ); # Working backwards here. # If someone has submitted a form with both the title and # author fields filled out, and the fields contain at least # one alphanumeric character if ( $cgi->param('title') && $cgi->param('title') =~ /\w/ && $cgi->param('author') && $cgi->param('author') =~ /\w/ ) { # Prepare our query ... my $stmt = $db->prepare( "INSERT INTO book (title,author) VALUES (?,?)" ) or die( "Can't prep INSERT because ", $db->errstr ); # And then run it, inserting the data from the form. $stmt->execute( $cgi->param('title'), $cgi->param('author') ) or die( "Can't exec INSERT because ", $stmt->errstr ); # Clean up after ourselves. $stmt->finish; # If they submitted the form w/o entering data for both # fields, tell them off ... } elsif ( $cgi->param ) { print $cgi->p( "You must fill in BOTH fields" ); } # Regardless of what the visitor has just done (or not done) # or whether they were successful, spit out the existing # contents of the the database. print $cgi->start_form, $cgi->start_table( { -border => 1 } ), $cgi->Tr( $cgi->th( [ "Title", "Author" ] ) ); # Yup, we prepare another query! my $stmt = $db->prepare( "SELECT title,author FROM book" ) or die( "Can't prep SELECT because ", $db->errstr ); # ... run it ... $stmt->execute or die( "Can't exec SELECT because ", $db->errstr ); my $books = 0; # ... and then iterate over the results it returns ... while( my $row = $stmt->fetchrow_arrayref ) { $books++; print $cgi->Tr( $cgi->td( $row ) ); } # Finally, include an input form so they can add more # entries themselves. print $cgi->Tr( $cgi->td( [ $cgi->textfield( -name => 'title' ), $cgi->textfield( -name => 'author' ), ], ), ), $cgi->end_table, $cgi->submit( -value => 'Add book!' ), $cgi->p( "There are ", ($books ? $books : "no"), " books in the db." ), $cgi->end_form, $cgi->end_html;</pre><HR></BLOCKQUOTE> [This message has been edited by Kanji (edited 07-14-2000).]
|