2 # $Id: persons.cgi,v 1.6 1999-04-22 05:54:53 ivan Exp $
3 # Copyright (c) 1999 Ivan Kohler. All rights reserved.
4 # This program is free software; you can redistribute it and/or modify it under
5 # the same terms as perl itself
8 use vars qw ( $data_source $user $password $table @fields
11 use subs qw( print_form );
13 use CGI::Carp qw(fatalsToBrowser);
16 $data_source = "DBI:mysql:technostate";
21 @fields = qw( PERSON_ID NAME EMAIL AFFILIATION HOMEPAGE );
25 $dbh = DBI->connect( $data_source, $user, $password )
26 or die "Cannot connect: ". $DBI::errstr;
28 unless ( $cgi->param('magic') ) { #first time through
30 my $sth = $dbh->prepare( "SELECT * FROM PERSONS" )
32 my $rv = $sth->execute;
33 die $sth->errstr unless $rv;
35 print $cgi->header( '-expires' => 'now' ),
36 $cgi->start_html('Person listing'),
37 $cgi->h1('Person listing'),
40 unless ( $sth eq '0E0' ) {
42 my @columns = @{ $sth->{'NAME'} };
44 print $cgi->start_table,
54 while ( $hashref = $sth->fetchrow_hashref ) {
56 $hash{'EMAIL'} = '<A HREF="mailto:'. $hash{'EMAIL'}. '">'.
57 $hash{'EMAIL'}. "</A>";
58 $hash{'HOMEPAGE'} = 'http://'. $hash{'HOMEPAGE'}
59 unless $hash{'HOMEPAGE'} =~ /^http\:\/\//;
60 $hash{'HOMEPAGE'} = '<A HREF="'. $hash{'HOMEPAGE'}. '">'.
61 $hash{'HOMEPAGE'}. "</A>";
62 print $cgi->tr( map { $cgi->td( $hash{$_} ) } @columns );
64 print $cgi->end_table;
68 $cgi->param('magic', 'new_form');
69 print '<P><A HREF="', $cgi->self_url, '">Add new person</A>';
74 } elsif ( $cgi->param('magic') eq 'new_form' ) {
75 $cgi->param('PERSON_ID', 0);
76 $cgi->param('magic', 'process_form');
77 &print_form( $cgi, "Add person" );
79 } elsif ( $cgi->param('magic') eq 'process_form' ) {
82 foreach $field ( @fields ) {
83 if ( $cgi->param( $field ) ) {
84 $cgi->param( $field ) =~ /^(.*)$/;
85 $cgi->param( $field, $1);
88 my $statement = 'INSERT INTO PERSONS ( '.
91 join( ', ', map { $dbh->quote($cgi->param($_)) } @fields ).
94 my $sth = $dbh->prepare($statement)
96 my $rv = $sth->execute;
97 die $sth->errstr unless $rv;
100 $url =~ s/^\/[\/]+$//;
101 print $cgi->redirect($url);
107 print $cgi->header( '-expires' => 'now' ),
108 $cgi->start_html($action),
111 $cgi->hidden( -name => 'PERSON_ID' ),
112 "Name: ", $cgi->textfield( -name => 'NAME' ), "<BR>",
113 "Email: ", $cgi->textfield( -name => 'EMAIL' ), "<BR>",
114 "Affiliation: ", $cgi->textfield( -name => 'AFFILIATION' ), "<BR>",
115 "Homepage: ", $cgi->textfield( -name => 'HOMEPAGE' ), "<BR>",
116 $cgi->hidden( -name => 'magic'),
117 $cgi->submit('Submit'),