Ups, havde lige lavet en fejl i koden, så det skal være sådan her i stedet:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <title>PHP/MySQL-afstemning</title>
- <style type="text/css">
- body {
- font-family:Verdana, Geneva, sans-serif;
- font-size:11px;
- }
- #header {
- font-size:12px;
- font-weight:bold;
- }
- </style>
- </head>
- <body>
- <?php
- $host = "localhost"; //din host (ofte localhost)
- $username = "?"; //dit brugernavn
- $password = "?"; //dit password
- $database = "?"; //navn på din database
-
- //opretter forbindelse til MySQL
- $db = mysql_connect($host, $username, $password);
- if(!$db) {
- print "Error - Could not connect to MySQL";
- exit;
- }
- //opretter forbindelse til databasen
- $er = mysql_select_db($database);
- if(!$er) {
- print "Error - Could not select the test database";
- exit;
- }
- $ip = $_SERVER["REMOTE_ADDR"];
-
- if(!empty($_POST['answer'])){
- mysql_query('INSERT INTO `afstemning` (`svarID`, `svar`, `ip`) VALUES (NULL, \''.$_POST['answer'].'\', \''.$ip.'\');');
- }
-
- //tjekker ip adresse for at se om brugeren tidligere har stemt
- if(mysql_num_rows(mysql_query('SELECT * FROM afstemning WHERE ip = "'.$ip.'"')) > 0){
- $totalVotes = mysql_num_rows(mysql_query('SELECT `svar` FROM `afstemning`'));
-
- echo "
- <p id='header'>Hvor skal træningslejren holdes?</p>
- <table>
- <tr>
- <td>Ølstykke - ";
- $votes0 = mysql_num_rows(mysql_query('SELECT `svar` FROM `afstemning` WHERE `svar` = 0'));
- $votes0Pct = round((($votes0/$totalVotes)*100),0);
-
- echo "<img src='billeder/afstemning_procent.gif' alt='".$votes0." stemmer' width='".$votes0Pct."px' height='10px' /> (".$votes0Pct." %)";
- echo "</td>
- </tr>
- <tr>
- <td>Roskilde - ";
- $votes1 = mysql_num_rows(mysql_query('SELECT `svar` FROM `afstemning` WHERE `svar` = 1'));
- $votes1Pct = round((($votes1/$totalVotes)*100),0);
-
- echo "<img src='billeder/afstemning_procent.gif' alt='".$votes1." stemmer' width='".$votes1Pct."px' height='10px' /> (".$votes1Pct." %)";
- echo "</td>
- </tr>
- <tr>
- <td>Holbæk - ";
- $votes2 = mysql_num_rows(mysql_query('SELECT `svar` FROM `afstemning` WHERE `svar` = 2'));
- $votes2Pct = round((($votes2/$totalVotes)*100),0);
-
- echo "<img src='billeder/afstemning_procent.gif' alt='".$votes2." stemmer' width='".$votes2Pct."px' height='10px' /> (".$votes2Pct." %)";
- echo "</td>
- </tr>
- <tr>
- <td>Birkerød - ";
- $votes3 = mysql_num_rows(mysql_query('SELECT `svar` FROM `afstemning` WHERE `svar` = 3'));
- $votes3Pct = round((($votes3/$totalVotes)*100),0);
-
- echo "<img src='billeder/afstemning_procent.gif' alt='".$votes3." stemmer' width='".$votes3Pct."px' height='10px' /> (".$votes3Pct." %)";
- echo "</td>
- </tr>
- <tr>
- <td>KB - ";
- $votes4 = mysql_num_rows(mysql_query('SELECT `svar` FROM `afstemning` WHERE `svar` = 4'));
- $votes4Pct = round((($votes4/$totalVotes)*100),0);
-
- echo "<img src='billeder/afstemning_procent.gif' alt='".$votes4." stemmer' width='".$votes4Pct."px' height='10px' /> (".$votes4Pct." %)";
- echo "</td>
- </tr>
- </table>
- </form>";
- } else {
- echo "
- <form action='poll.php' method='post'>
- <p id='header'>Hvor skal træningslejren holdes?</p>
- <table width='120px'>
- <tr>
- <td>Ølstykke</td>
- <td align='right'><input type='radio' name='answer' value='0' /></td>
- </tr>
- <tr>
- <td>Roskilde</td>
- <td align='right'><input type='radio' name='answer' value='1' /></td>
- </tr>
- <tr>
- <td>Holbæk</td>
- <td align='right'><input type='radio' name='answer' value='2' /></td>
- </tr>
- <tr>
- <td>Birkerød</td>
- <td align='right'><input type='radio' name='answer' value='3' /></td>
- </tr>
- <tr>
- <td>KB</td>
- <td align='right'><input type='radio' name='answer' value='4' /></td>
- </tr>
- <tr>
- <td></td>
- <td align='right'><input type='submit' value='Stem' name='stem' /></td>
- </tr>
- </table>
- </form>";
- }
- ?>
-
-
- </body>
- </html>
- </html>
At den starter fra 4, skyldes at der lå 4 stemmer i databasen. Du "resetter" den ved at gå ind på tabellen og trykke på fanebladet "Tøm"
@Nørden: Ja, jeg er klar over at det ikke er den mest elegante løsning jeg biksede sammen
Indlæg senest redigeret d. 22.02.2009 20:47 af Bruger #10572