Christoph Burghardt

 Start   

Umfrage

Tabelle umfrage in der Datenbank xy anlegen

entweder DOS-Eingabeaufforderung

mysql> create table umfrage (
id int (10) unsigned not null auto-increment,
frage varchar (255) not null default ' ',
antwort1 varchar (255) not null default ' ',
antwort2 varchar (255) not null default ' ',
antwort3 varchar (255) not null default ' ',
abstimmung1 int(10) unsigned not null default '0',
abstimmung2 int(10) unsigned not null default '0',
abstimmung3 int(10) unsigned not null default '0',
primary key (id)
) type=myisam;

oder Erstellung der Tabelle über php-Skript


tabelleanlegen.php

<?php
include ('config.php');
$a= mysql_query("create table umfrage (
id int (10) unsigned not null auto_increment,
frage varchar (255) not null default ' ',
antwort1 varchar (255) not null default ' ',
antwort2 varchar (255) not null default ' ',
antwort3 varchar (255) not null default ' ',
abstimmung1 int(10) unsigned not null default '0',
abstimmung2 int(10) unsigned not null default '0',
abstimmung3 int(10) unsigned not null default '0',
primary key (id)
) type=myisam; ");

?>


config.php

<?php
$server= "localhost";
$user= "root";
$passwort= "root";
$datenbank= "xy";
$verbindung= mysql_connect ($server, $user, $passwort) or die ("Es konnte keine
Verbindung zum Server hergestellt werden.");
mysql_select_db ($datenbank) or die ("Die Datenbank existiert nicht.");

?>


styles.css

h2
{
font-weight: bold;
font-size: 15px;
text-transform: uppercase;
color: #000000;
line-height: 15px;
font-family: verdana, arial, helvetcia
}

div
{
font-weight: bold;
font-size: 10px;
text-transform: uppercase;
color: #039006;
line-height: 15px;
font-family: verdana, arial, helvetcia
}

input
{
background-color: #ffffff;
border-left: 1px solid #000000;
border-right: 1px solid #000000;
border-top: 1px solid #000000;
border-bottom: 1px solid #000000
}



eingabe.php

<html>
<head>
<title>
Erstellung der Umfrage
</title>
<link rel= "StyleSheet" type="text/css" href= "styles.ccs">
</head>

<body>

<?php
$a =$_GET ["submit"];
$b =$_GET ["frage"];
$c =$_GET ["antwort1"];
$d =$_GET ["antwort2"];
$e =$_GET ["antwort3"];
if (!$a)

// Die if-Bedingung ist notwendig, sonst wird bei jedem Aufruf des Skripts ein leerer Datensatz erzeugt.
// Wenn $a nicht vorhanden ist (es wurde nur das Skript aufgerufen und nicht auf den Button
// "abschicken" geklickt) erscheint das Eingabeformular. Wird dann auf "abschicken" geklickt, erhält $a
// einen Wert und die Anweisung nach else wird ausgeführt.

{

?>

<table border="0" cellspacing="5" cellpadding="5">
<tr>
<td>
<form action = "eingabe.php" method="get">

<table cellspacing= "5">
<tr>
<td>
<div> Umfrage-Titel </div>
</td>

<td>
<input type = "text" name="frage" size="26">
</td>
</tr>
<tr>
<td>
<div>Antwort1 </div>
</td>
<td>
<input type = "text" name="antwort1" size="26">
</td>
</tr>
<tr>
<td>
<div>Antwort2 </div>
</td>
<td>
<input type = "text" name="antwort2" size="26">
</td>
</tr>
<tr>
<td>
<div>Antwort3 </div>
</td>
<td>
<input type = "text" name="antwort3" size="26">
</td>
</tr>
<tr>

<td align="right" colspan="2">
<input type ="submit" name="submit" value="Umfrage abschicken">

</td>
</tr>
</table>
</form>
</td>
</tr>
</table>

<?php

}
else
{

include ('config.php');
$f= "insert into umfrage (frage, antwort1, antwort2, antwort3) values
('$b','$c','$d','$e')";
$g= mysql_query($f);

}

?>

</body>
</html>


umfrage.php

<html>
<head>
<title>
Hier erfolgt die Abstimmung
</title>
<link rel= "StyleSheet" type="text/css" href= "styles.css">
</head>

<body>

<?php
$a =$_GET ["submit"];
$b =$_GET ["frage"];
$antwort=$_GET ["antwort"];
$c =$_GET ["antwort1"];
$d =$_GET ["antwort2"];
$e =$_GET ["antwort3"];
$i =$_GET ["id"];

include ('config.php');
$y= "select id, frage, antwort1, antwort2, antwort3 from umfrage order by id desc";
$z= mysql_db_query(xy,$y);
$p= mysql_fetch_row($z); //Array
list($i, $b, $c, $d, $e) = $p; // Zuweisung von Variablen list-Funktion

?>

<form method="get" action= "abstimmung.php">

<?php
echo '<b>';
echo $b;
echo '</b>';

?>

<table width="200">
<tr>
<td>
<div>

<?php
echo $c;

?>

</div>
</td>
<td>
<input type="radio" name="antwort" value="1">
</td>
</tr>
<tr>
<td>
<div>

<?php
echo $d;

?>

</div>
</td>
<td>
<input type="radio" name="antwort" value="2">
</td>
</tr>
<tr>
<td>
<div>

<?php
echo $e;

?>

</div>
</td>
<td>
<input type="radio" name="antwort" value="3">
</td>
</tr>
</table>

<input type="hidden" name="id" value="

<?php
echo $i;

?>
">

<br>

<input type="submit" name="submit" value="jetzt abstimmen">

</form>

<form method="get" action= "zwischenergebnis.php">

<input type="hidden" name="id" value="<?php echo $i; ?>">

<input type="submit" name="submit" value="Zwischenergebnis">

</form>

</body>
</html>


abstimmung.php

<?php
include ('config.php');
$a =$_GET ["submit"];
$b =$_GET ["frage"];
$antwort=$_GET ["antwort"];
$c =$_GET ["antwort1"];
$d =$_GET ["antwort2"];
$e =$_GET ["antwort3"];
$i =$_GET ["id"];
$zuletzt=$_COOKIE["zuletzt"];

if (!$a or !$antwort)

{

?>

<html>
<head>
<link rel= "StyleSheet" type="text/css" href= "styles.css">
</head>

<body>

<b> Fehler! keine Auswahl getroffen
<br>
<br>
<a href="umfrage.php"> zurück zu umfrage.php
</a>
</b>

<?php

}
elseif ($zuletzt==$i)
{

?>

</body>
</html>

<html>
<head>
<link rel= "StyleSheet" type="text/css" href= "styles.css">
</head>
<body>
<b>Abstimmung erfolgte bereits
<br>
<br>
<a href="umfrage.php"> zurück zu umfrage.php
</a>

</b>

</body>
</html>

<?php

}
else
{

setCookie("zuletzt",$i);

?>
<html>
<head>
<link rel= "StyleSheet" type="text/css" href= "styles.css">
</head>
<body>

<?php
include ('config.php');
$feld="abstimmung" . $antwort; // zum Beispiel: abstimmung2
$update= "update umfrage set $feld= $feld +1 where id= $i";
$query=mysql_query($update);

echo '<b>Eingabe erfolgreich</b><br>';

$anfrage= "select frage, antwort1, antwort2, antwort3, abstimmung1, abstimmung2, abstimmung3 from umfrage
where id=$i";

$result=mysql_db_query(xy,$anfrage);
list($frage, $antwort1, $antwort2, $antwort3, $abstimmung1, $abstimmung2, $abstimmung3)
= mysql_fetch_row($result);

$gesamt= $abstimmung1 + $abstimmung2 + $abstimmung3;
$prozent1= round(($abstimmung1/$gesamt)*100,2);
$prozent2= round(($abstimmung2/$gesamt)*100,2);
$prozent3= round(($abstimmung3/$gesamt)*100,2);

echo '<br>';
echo '<b>';
echo $frage;
echo '<br>';
echo 'Abgegebene Stimmen: ';
echo $gesamt;
echo '<br>';
echo $antwort1;
echo ': ';
echo $prozent1;
echo '%';
echo '<br>';
echo $antwort2;
echo ': ';
echo $prozent2;
echo '%';
echo '<br>';
echo $antwort3;
echo ': ';
echo $prozent3;
echo '%';
echo '</b>';

echo '<br> <br>
<b>
<a href="umfrage.php"> zurück zu umfrage.php
</a>
</b>';

}

?>

</body>
</html>


archiv.php

<html>
<head>
<title>
Ergebnisse aller Umfragen anzeigen
</title>
<link rel= "StyleSheet" type="text/css" href= "styles.css">
</head>

<body>

<?php
include ('config.php');
$anfrage= "select frage, antwort1, antwort2, antwort3, abstimmung1, abstimmung2, abstimmung3 from umfrage ";

$result=mysql_db_query(xy,$anfrage);

while(list($frage, $antwort1, $antwort2, $antwort3, $abstimmung1, $abstimmung2, $abstimmung3)
= mysql_fetch_row($result))

{

$gesamt= $abstimmung1 + $abstimmung2 + $abstimmung3;

if ($gesamt > 0)

{

$prozent1= round(($abstimmung1/$gesamt)*100,2);
$prozent2= round(($abstimmung2/$gesamt)*100,2);
$prozent3= round(($abstimmung3/$gesamt)*100,2);

echo '<br';
echo '<b>';
echo $frage;
echo '<br>';
echo 'Abgegebene Stimmen: ';
echo $gesamt;
echo '<br>';
echo $antwort1;
echo ': ';
echo $prozent1;
echo '%';
echo '<br>';
echo $antwort2;
echo ': ';
echo $prozent2;
echo '%';
echo '<br>';
echo $antwort3;
echo ': ';
echo $prozent3;
echo '%';
echo '</b>';
echo '<br>';

}
else
{

$prozent1=$abstimmung1;
$prozent2=$abstimmung2;
$prozent3=$abstimmung3;

echo '<br';
echo '<b>';
echo $frage;
echo '<br>';
echo 'Abgegebene Stimmen: ';
echo $gesamt;
echo '<br>';
echo $antwort1;
echo ': ';
echo $prozent1;
echo '%';
echo '<br>';
echo $antwort2;
echo ': ';
echo $prozent2;
echo '%';
echo '<br>';
echo $antwort3;
echo ': ';
echo $prozent3;
echo '%';
echo '</b>';

echo '<br>';

}

}

?>

</body>
</html>


zwischenergebnis.php

<html>
<head>
<title>
Zwischenergebnis der aktuellen Umfrage anzeigen
</title>
</head>
<body>

<?php
include ('config.php');
$i =$_GET ["id"];

$anfrage= "select frage, antwort1, antwort2, antwort3, abstimmung1, abstimmung2, abstimmung3 from umfrage
where id= $i";
$result=mysql_db_query(xy,$anfrage);
list($frage, $antwort1, $antwort2, $antwort3, $abstimmung1, $abstimmung2, $abstimmung3)
= mysql_fetch_row($result);

$gesamt= $abstimmung1 + $abstimmung2 + $abstimmung3;

if ($gesamt>0)

{

$prozent1= round(($abstimmung1/$gesamt)*100,2);
$prozent2= round(($abstimmung2/$gesamt)*100,2);
$prozent3= round(($abstimmung3/$gesamt)*100,2);

echo '<br>';
echo '<b>';
echo $frage;
echo '<br>';
echo 'Abgegebene Stimmen: ';
echo $gesamt;
echo '<br>';
echo $antwort1;
echo ': ';
echo $prozent1;
echo '%';
echo '<br>';
echo $antwort2;
echo ': ';
echo $prozent2;
echo '%';
echo '<br>';
echo $antwort3;
echo ': ';
echo $prozent3;
echo '%';
echo '</b>';

}
else
{

echo 'es hat noch niemand abgestimmt';

}

?>

<br>
<br>
<b>
<a href="umfrage.php"> zurück zu umfrage.php </a>
</b>

</body>
</html>