#!/usr/bin/perl

use CGI;
use DBI;

my $db = 'firma';
my $host = 'drcomp';
my $user = 'apache';
my $password = '';
my $table = 'kunden';

my $query = CGI->new();

my $vorname = $query->param('vorname');
my $name = $query->param('name');
my $email = $query->param('email');
my $firma = $query->param('firma');
my $strasse = $query->param('strasse');
my $plz = $query->param('plz');
my $ort = $query->param('ort');
my $tel = $query->param('tel');
my $fax = $query->param('fax');
my $funk = $query->param('funk');

$dbh = DBI->connect("DBI:Pg:dbname=$db;host=$host",$user, $password,
{RaiseError => 1});

# ------------------------------------------------------------------------

sub schreibe {                          # gibt eine datei aus
        open(DATEI, "<$_[0]") || printf("missing $_[0]: $! \n");
        while (<DATEI>) {
        print;
        }
        close(DATEI);
  }

# ------------------------------------------------------------------------

sub sql {                       # prepare SQL and perform checking

        $sth = $dbh->prepare($_[0]);
        if (!$sth) {
                die "Error:" . $dbh->errstr . "\n";
        }
        if (!$sth->execute) {
        die "Error:" . $sth->errstr . "\n";
        }
    }

# ------------------------------------------------------------------------

sub briefkopf {
        print $query->header(),
              $query->start_html(-bgcolor=>'#ffffff',
               -title=>'[ Web-bill | Neukundeneintrag ]');

        print <<HTML_CODE;
<center>
<font size=+3>Web-basierendes Rechnungssystem<br><br>
HTML_CODE
        }

# ------------------------------------------------------------------------
# ------------------------------------------------------------------------

sub db_insert {			# insert the client into database
	sql("INSERT INTO $table (vorname,name,firma,strasse,ort,plz,tel,\
	    fax,funk,email) values('$vorname','$name','$firma','$strasse',\
	    '$ort','$plz','$tel','$fax','$funk','$email')");

	}

# ------------------------------------------------------------------------

sub db_confirm {		# print out the new db-entry

	sql("SELECT * from $table WHERE kundennummer=currval('kundenID');");
	print $query->p("Registriert wie folgt:");
	
	my $namen = $sth->{'NAME'};
	my $anzFelder = $sth->{'NUM_OF_FIELDS'};

	print '<table border="0">';
	while (my $ref = $sth->fetchrow_arrayref) {
		for (my $i = 0; $i < $anzFelder; $i++) {
		print("<tr><td align=\"right\">$$namen[$i]:\
		<td align=\"left\">$$ref[$i]</tr>");
		}
		}
	print '</table>', $query->end_html();
	}
	
# ------------------------------------------------------------------------
# ------------------------------------------------------------------------
# ------------------------------------------------------------------------

briefkopf();
db_insert();
db_confirm();
