I've created a poll script which looks like the following:
poll.php
<?php require_once('Connections/andypops.php');
mysql_select_db($database_andypops, $andypops);
$query_poll = "SELECT * FROM poll";
$poll = mysql_query($query_poll, $andypops) or die(mysql_error());
$row_poll = mysql_fetch_assoc($poll);
$totalRows_poll = mysql_num_rows($poll);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body class="pollbd">
<form id="poll" name="poll" method="post" action="pollvote.php">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" id="question"><?php echo $row_poll['question']; ?></td>
</tr>
<tr>
<td width="3%"><input name="option" type="radio" value="votes1" checked="checked" /></td>
<td width="97%"><?php echo $row_poll['answer1']; ?></td>
</tr>
<tr>
<td><input type="radio" name="option" value="votes2" /></td>
<td><?php echo $row_poll['answer2']; ?></td>
</tr>
<tr>
<td><input type="radio" name="option" value="votes3" /></td>
<td><?php echo $row_poll['answer3']; ?></td>
</tr>
<tr>
<td><input type="radio" name="option" value="votes4" /></td>
<td><?php echo $row_poll['answer4']; ?></td>
</tr>
<tr>
<td colspan="2" align="center"><input name="Submit" type="submit" class="formel" value="Vote" /></td>
</tr>
</table>
</form>
</body>
</html>
<?php
mysql_free_result($poll);
?>
pollvote.php
<?php require_once('Connections/andypops.php');
mysql_select_db($database_andypops, $andypops);
$query_poll = "SELECT id, votes1, votes2, votes3, votes4, total FROM poll";
$poll = mysql_query($query_poll, $andypops) or die(mysql_error());
$row_poll = mysql_fetch_assoc($poll);
$totalRows_poll = mysql_num_rows($poll);
if ($_POST['option'] == "votes1") {
$votes1 = $row_poll['votes1'] +1;
$total = $row_poll['total'] +1;
mysql_query("UPDATE `poll` SET `votes1` = '$votes1', `total` = '$total' WHERE `id` = '1' LIMIT 1", $andypops);
include("pollresults.php");
}
if ($_POST['option'] == "votes2") {
$votes2 = $row_poll['votes2'] +1;
$total = $row_poll['total'] +1;
mysql_query("UPDATE `poll` SET `votes2` = '$votes2', `total` = '$total' WHERE `id` = '1' LIMIT 1", $andypops);
include("pollresults.php");
}
if ($_POST['option'] == "votes3") {
$votes3 = $row_poll['votes3'] +1;
$total = $row_poll['total'] +1;
mysql_query("UPDATE `poll` SET `votes3` = '$votes3', `total` = '$total' WHERE `id` = '1' LIMIT 1", $andypops);
include("pollresults.php");
}
if ($_POST['option'] == "votes4") {
$votes4 = $row_poll['votes4'] +1;
$total = $row_poll['total'] +1;
mysql_query("UPDATE `poll` SET `votes4` = '$votes4', `total` = '$total' WHERE `id` = '1' LIMIT 1", $andypops);
include("pollresults.php");
}
mysql_free_result($poll);
?>
/b]pollresults.php[/b]
<?php require_once('Connections/andypops.php'); ?>
<?php
mysql_select_db($database_andypops, $andypops);
$query_poll = "SELECT * FROM poll";
$pollb = mysql_query($query_poll, $andypops) or die(mysql_error());
$row_poll = mysql_fetch_assoc($pollb);
$totalRows_poll = mysql_num_rows($pollb);
if($row_poll["votes1"] != 0) {
$votepercent1 = Round(($row_poll["votes1"] / $row_poll["total"]) * 100) . "%";
} else {
$votepercent1 = 0 . "%";
}
if($row_poll["votes2"] != 0) {
$votepercent2 = Round(($row_poll["votes2"] / $row_poll["total"]) * 100) . "%";
} else {
$votepercent2 = 0 . "%";
}
if($row_poll["votes3"] != 0) {
$votepercent3 = Round(($row_poll["votes3"] / $row_poll["total"]) * 100) . "%";
} else {
$votepercent3 = 0 . "%";
}
if($row_poll["votes4"] != 0) {
$votepercent4 = Round(($row_poll["votes4"] / $row_poll["total"]) * 100) . "%";
} else {
$votepercent4 = 0 . "%";
}
?>
<link href="style.css" rel="stylesheet" type="text/css" />
<body class="pollbd">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td id="question"><?php echo $row_poll['question']; ?></td>
</tr>
<tr>
<td id="answer"><?php echo $row_poll['answer1']; ?></td>
</tr>
<tr>
<td id="bar"><img src="images/pollbar.jpg" width="<?php echo $votepercent1 ?>" height="5"></td>
</tr>
<tr>
<td id="answer"><?php echo $row_poll['answer2']; ?></td>
</tr>
<tr>
<td id="bar"><img src="images/pollbar.jpg" width="<?php echo $votepercent2 ?>" height="5"></td>
</tr>
<tr>
<td id="answer"><?php echo $row_poll['answer3']; ?></td>
</tr>
<tr>
<td id="bar"><img src="images/pollbar.jpg" width="<?php echo $votepercent3 ?>" height="5"></td>
</tr>
<tr>
<td id="answer"><?php echo $row_poll['answer4']; ?></td>
</tr>
<tr>
<td id="bar"><img src="images/pollbar.jpg" width="<?php echo $votepercent4 ?>" height="5"></td>
</tr>
<tr>
<td id="answer">Total Votes: <?php echo $row_poll['total']; ?></td>
</tr>
</table>
</body>
<?php
mysql_free_result($pollb);
?>
What I need to do is somehow register which people have already voted on the poll and if they have already voted, automatically redirect them to pollresults.php (poll.php is embedded in another page by default). I tried setting cookies and suchlike but it just froze everything up and gave me about a hundred million errors. Thanks!
-Andy
