#!/usr/local/bin/perl

use Mysql;
use CGI;
use Data::Dumper;

$cgi = new CGI;
$DATA = $cgi->Vars;

print "Content-type: text/html\n\n";
print qq~<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Mitgliederverwaltung</title>
		<style type="text/css">
		td { border: 1px solid black; }
		th { border: 1px solid black; }
		table {
			border: 1px solid black;
			margin-bottom: 2em;}
		fieldset {
			border: 1px solid black;
		}
		.floatleft { float: left; }
	</style>
</head>
<body>
	<h1>Mitgliederverwaltung</h1>
~;

$host = "localhost";
$database = "testdb";
$username = "testuser";
$pw = "asdf";

$db = Mysql->connect($host, $database, $username, $pw);
$db->selectdb($database);


if ($DATA->{"action"} eq "insert") {
	$query = "INSERT INTO mitglieder (\
		`id`,\
		`vorname`,\
		`name`,\
		`istkompetent`,\
		`gebdat`\
		)\
		VALUES (\
		NULL, '".	$DATA->{"vorname"}."', '".$DATA->{"name"}."', '".
		$DATA->{"kompetent"}."', '".
		$DATA->{"geby"}."-".$DATA->{"gebm"}."-".$DATA->{"gebd"}."'\
		);";
	$db->query($query);
}

if ($DATA->{"action"} eq "delete") {
	$query = "DELETE FROM mitglieder WHERE id = ".$DATA->{"id"}.";";
	$db->query($query);
};

my $a = $db->query("SELECT * FROM mitglieder");
my $fields = $a->numfields();
print "	<table>\n";
print '		<tr><th>ID</th><th>Vorname</th><th>Nachname</th><th>Kompetent?</th><th>Geburtstag</th></tr>';
while (my @result = $a->fetchrow()) {
	print "		<tr>";
	for (my $i = 0; $i < $fields; $i++) {
		print "<td>$result[$i]</td>";
	}
	print "</tr>\n";
}
print "	</table>\n";


print qq~	<form method="get" action="mitglieder.pl">
		<fieldset class="floatleft">
		<legend>Mitglied hinzuf&uuml;gen</legend>
		Vorname:<br />
		<input name="vorname" type="text" /><br />
		Name:<br />
		<input name="name" type="text" /><br />
		Ist kompetent:<br />
		Ja
		<input name="kompetent" type="radio" value="1" />
		Nein
		<input name="kompetent" type="radio" value="0" /><br />
		Geburtstag:
		<input name="gebd" type="text" maxlength="2" size="2" />
		<input name="gebm" type="text" maxlength="2" size="2" />
		<input name="geby" type="text" maxlength="4" size="4" />
		<input name="action" value="insert" type="hidden" />
		<input type="submit" value="Insert" />
		</fieldset>
	</form>
~;

print qq~	<form method="get" action="mitglieder.pl">
		<fieldset>
		<legend>Mitglied l&ouml;schen</legend>
		ID:<br />
		<input name="id" type="text" /><br />
		<input name="action" value="delete" type="hidden" />
		<input type="submit" value="Delete" />
		</fieldset>
	</form>
~;

print qq~</body>
</html>
~;
