2 # $Id: shows.cgi,v 1.1 1999-04-23 06:34:07 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( SHOW_ID PERSON_ID SHOWSTART DURATION STUDIO_ID );
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 $table" )
32 my $rv = $sth->execute;
33 die $sth->errstr unless $rv;
35 print $cgi->header( '-expires' => 'now' ),
36 $cgi->start_html('Show listing'),
37 $cgi->h1('Show listing'),
40 unless ( $sth eq '0E0' ) {
42 my @columns = @{ $sth->{'NAME'} };
44 print $cgi->start_table,
54 while ( $hashref = $sth->fetchrow_hashref ) {
56 print $cgi->tr( map { $cgi->td( $hash{$_} ) } @columns );
58 print $cgi->end_table;
62 $cgi->param('magic', 'new_form');
63 print '<P><A HREF="', $cgi->self_url, '">Add new show</A>';
68 } elsif ( $cgi->param('magic') eq 'new_form' ) {
69 $cgi->param('SHOW_ID', 0);
70 $cgi->param('magic', 'process_form');
71 &print_form( $cgi, "Add show" );
73 } elsif ( $cgi->param('magic') eq 'process_form' ) {
76 foreach $field ( @fields ) {
77 if ( $cgi->param( $field ) ) {
78 $cgi->param( $field ) =~ /^(.*)$/;
80 if ( (DBI::looks_like_number($param))[0] ) {
81 $cgi->param( $field, $param );
83 $cgi->param( $field, $dbh->quote($param) );
87 my $statement = "INSERT INTO $table ( ".
90 join( ', ', map { $cgi->param($_) } @fields ).
93 my $sth = $dbh->prepare($statement)
95 my $rv = $sth->execute;
96 die $sth->errstr unless $rv;
99 $url =~ s/^\/[\/]+$//;
100 print $cgi->redirect($url);
107 $cgi->start_html($action),
110 $cgi->hidden( -name => 'SHOW_ID' ),
111 "Person_ID: ", $cgi->textfield( -name => 'PERSON_ID' ), "<BR>",
112 "Show start: ", $cgi->textfield( -name => 'SHOWSTART' ), "<BR>",
113 "Duration: ", $cgi->textfield( -name => 'DURATION' ), "<BR>",
114 "Studio_ID: ", $cgi->textfield( -name => 'STUDIO_ID' ), "<BR>",
115 $cgi->hidden( -name => 'magic'),
116 $cgi->submit('Submit'),