summaryrefslogtreecommitdiff
path: root/cgi/persons.cgi
blob: e06212473c870d8caced8afdc511713ca4ac95b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/perl -Tw
# $Id: persons.cgi,v 1.3 1999-04-22 04:27:47 ivan Exp $
# Copyright (c) 1999 Ivan Kohler.  All rights reserved.
# This program is free software; you can redistribute it and/or modify it under
# the same terms as perl itself

use strict;
use vars qw ( $data_source $user $password $table @fields
              $cgi $dbh
            );
use subs qw( print_form );
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use DBI;

$data_source = "DBI:mysql:technostate";
$user = "agent";
$password = "t3chno";

$table = "PERSONS";
@fields = qw( PERSON_ID NAME EMAIL AFFILIATION );

$cgi = new CGI;

$dbh = DBI->connect( $data_source, $user, $password )
  or die "Cannot connect: ". $DBI::errstr;

unless ( $cgi->param('magic') ) { #first time through

  my $sth = $dbh->do( "SELECT * FROM PERSONS" ) or die $dbh->errstr;

  print $cgi->header,
        $cgi->start_html('Person listing'),
        $cgi->h1('Person listing'),
  ;

  unless ( $sth eq '0E0' ) {

    print $cgi->start_table,
          $cgi->tr(
            map {
              $cgi->th($_)
            } @{$sth->{NAME}}
          ),
          map {
            $cgi->tr(
              map {
                $cgi->td( $_ )
              } @{ $_ }
            )
          } @{ $sth->fetchall_arrayref },
          $cgi->end_table,
    ;
  }

  $cgi->param('magic', 'new_form');
  print '<P><A HREF="', $cgi->self_url, '">Add new person</A>';
  print $cgi->end_html;

  exit;

} elsif ( $cgi->param('magic') eq 'new_form' ) {
  $cgi->param('PERSON_ID', 0);
  $cgi->param('magic', 'process_form');
  &print_form( $cgi, "Add person" );
  exit;
} elsif ( $cgi->param('magic') eq 'process_form' ) {
  foreach $field ( @fields ) {
    if $cgi->param( $field ) 

  my $statement = 'INSERT INTO PERSONS ( ', 
                  join(', ', @fields ),
                  ' ) VALUES ( ',
                  join( ', ', map { $cgi->param($_) } @fields ), 
                  ' )'
  ;
  my $sth = $dbh->prepare($statement)
    or die $dbh->errstr;
  my $rv = $sth->execute;
  die $sth->errstr unless $rv;

  print $cgi->header, 
        "person added?"
  ;
}

sub print_form {
  my $cgi = shift;
  my $action = shift;
  print $cgi->header,
        $cgi->start_html($action),
        $cgi->h1($action),
        $cgi->start_form,
        $cgi->hidden( -name => 'PERSON_ID' ),
        "Name: ", $cgi->textfield( -name => 'NAME' ), "<BR>", 
        "Email: ", $cgi->textfield( -name => 'EMAIL' ), "<BR>", 
        "Affiliation: ", $cgi->textfield( -name => 'AFFILIATION' ), "<BR>",
        $cgi->hidden( -name => 'magic'),
        $cgi->submit('Submit'),
        $cgi->end_form,
        $cgi->end_html;
  ;
}