Hi,
I am currently writing PHP code for some polling software. When someone
votes, it stores their IP address in the database. From then on, they
cannot vote in that particular poll, they only view the results. There
are several polls in the database, and there is a field called
"status". When it is 0, the poll is "active" and can be voted upon.
When it is 1, it is archived and one can only view the results.
But, there seems to be some kind of off-by one error. Here's an example
of how it goes. Say that I have two active polls, poll 1 and poll 2. If
I vote in poll 1, it displays what the results were prior to voting.
Then, if I vote in poll 2, it shows the correct results for poll 1, but
poll 2 still appears like you can vote in it. I have to cast a second
vote in poll 2 for the results to display. I hope that this makes
sense!
Anyway, here is my code. I am stumped; does anyone have any ideas?
Thanks in advance!!
<html>
<!-- Copyright @2004 -->
<!-- do whatever you want with it, just retain the copyright -->
<head>
<?php
// The file with various global settings
include "include.ph p";
// create connection
$conn = mysql_connect($ hostname, $username, $password);
if (!$conn) {
die ("Couldn't connect to server : " . mysql_error());
}
// select database
$db = mysql_select_db ($database, $conn);
if (!$db) {
die ("Couldn't select $database : " . mysql_error());
}
echo "</head>";
echo "<body>";
/* Create an array to mark active polls
so that we don't have to iterate through
the entire database table */
// Make array of all the active polls
$sql_active = "SELECT poll_ID FROM poll_titles WHERE status=0";
$sql_active_res ult = mysql_query($sq l_active,$conn) ;
if (!$sql_active_r esult) {
die ("Couldn't execute query : " . mysql_error());
}
$j = 0; // keeps track of array index
while ($row = mysql_fetch_arr ay($sql_active_ result)) {
$arr_active[$j] = $row["poll_ID"];
$j++;
}
/* Print all the active polls */
// Variable for number of array items, to use for the for loop
$arr_max = $j;
for ($i=0; $i < $arr_max; $i++) {
// Get the poll title
$sql_title = "SELECT title AS title FROM poll_titles WHERE
poll_ID=$arr_ac tive[$i]";
$sql_title_resu lt = mysql_query($sq l_title,$conn);
if (!$sql_title_re sult) {
die ("Couldn't execute query : " . mysql_error());
}
$row = mysql_fetch_arr ay($sql_title_r esult);
$title = $row["title"];
if ($title) {
echo "<p><table bgcolor='#FF00A A'><tr><td align='center'
colspan='2'><b> $title</b></td></tr><tr><td><p></p></td></tr>";
// See if user has already voted
$sql_ip = "SELECT COUNT(*) AS count FROM visited_ips WHERE
ip='$REMOTE_ADD R' AND poll_ID=$arr_ac tive[$i]";
$sql_ip_result = mysql_query($sq l_ip,$conn);
if (!$sql_ip_resul t) {
die ("Couldn't execute query : " . mysql_error());
}
$row = mysql_fetch_arr ay($sql_ip_resu lt);
$count = $row["count"];
// Get options and votes
$sql_options = "SELECT options, votes, id, poll_ID FROM poll WHERE
poll_ID=$arr_ac tive[$i]";
$sql_options_re sult = mysql_query($sq l_options,$conn );
if (!$sql_options_ result) {
die ("Couldn't execute query : " . mysql_error());
}
if (($count == 0) && ($_POST["hidden_id"] == $arr_active[$i])) {
/* See which value is checked */
// which poll was submitted?
$cur_poll = $_POST["hidden_id"];
$tmp = "group" . $cur_poll;
$val = $_POST[$tmp];
$sql_update = "UPDATE poll SET votes = votes + 1 WHERE ID = $val";
// execute the query
mysql_query($sq l_update);
// add ip to visited_ips
$sql_visited = "INSERT INTO visited_ips (ip, poll_ID) VALUES
('$REMOTE_ADDR' , $cur_poll)";
mysql_query($sq l_visited);
echo "<tr><td align='left'><u >Option</u></td><td
align='right'>< u>Votes</u></td></tr>";
while ($row = mysql_fetch_arr ay($sql_options _result)) {
$option = $row["options"];
$votes = $row["votes"];
echo "<tr><td align='left'>$o ption: </td><td
align='right'>$ votes</td></tr>";
}
echo "</table></p>";
}
// Display the poll if the user has not yet voted
else if (($count == 0) && ($_POST["hidden_id"] != $arr_active[$i])) {
echo "<form action='pollkit ty.php' method='post'>" ;
while ($row = mysql_fetch_arr ay($sql_options _result)) {
$option = $row["options"];
$id = $row["id"];
$poll_ID = $row["poll_ID"];
$groupname = "group" . $poll_ID;
echo "<tr><td align='left'><i nput type='radio' name='$groupnam e'
value='$id'>$op tion</td></tr>";
}
echo "<tr><td align='right'>
<input type='hidden' name='hidden_id ' value='$poll_ID '>
<input type='Submit' value='submit'> </td></tr>
</form>
</table></p>";
}
// Otherwise, if the user has already voted, display results
else {
echo "<tr><td align='left'><u >Option</u></td><td
align='right'>< u>Votes</u></td></tr>";
while ($row = mysql_fetch_arr ay($sql_options _result)) {
$option = $row["options"];
$votes = $row["votes"];
echo "<tr><td align='left'>$o ption: </td><td
align='right'>$ votes</td></tr>";
}
echo "</table></p>";
}
}
}
echo "</body></html>";
?>
I am currently writing PHP code for some polling software. When someone
votes, it stores their IP address in the database. From then on, they
cannot vote in that particular poll, they only view the results. There
are several polls in the database, and there is a field called
"status". When it is 0, the poll is "active" and can be voted upon.
When it is 1, it is archived and one can only view the results.
But, there seems to be some kind of off-by one error. Here's an example
of how it goes. Say that I have two active polls, poll 1 and poll 2. If
I vote in poll 1, it displays what the results were prior to voting.
Then, if I vote in poll 2, it shows the correct results for poll 1, but
poll 2 still appears like you can vote in it. I have to cast a second
vote in poll 2 for the results to display. I hope that this makes
sense!
Anyway, here is my code. I am stumped; does anyone have any ideas?
Thanks in advance!!
<html>
<!-- Copyright @2004 -->
<!-- do whatever you want with it, just retain the copyright -->
<head>
<?php
// The file with various global settings
include "include.ph p";
// create connection
$conn = mysql_connect($ hostname, $username, $password);
if (!$conn) {
die ("Couldn't connect to server : " . mysql_error());
}
// select database
$db = mysql_select_db ($database, $conn);
if (!$db) {
die ("Couldn't select $database : " . mysql_error());
}
echo "</head>";
echo "<body>";
/* Create an array to mark active polls
so that we don't have to iterate through
the entire database table */
// Make array of all the active polls
$sql_active = "SELECT poll_ID FROM poll_titles WHERE status=0";
$sql_active_res ult = mysql_query($sq l_active,$conn) ;
if (!$sql_active_r esult) {
die ("Couldn't execute query : " . mysql_error());
}
$j = 0; // keeps track of array index
while ($row = mysql_fetch_arr ay($sql_active_ result)) {
$arr_active[$j] = $row["poll_ID"];
$j++;
}
/* Print all the active polls */
// Variable for number of array items, to use for the for loop
$arr_max = $j;
for ($i=0; $i < $arr_max; $i++) {
// Get the poll title
$sql_title = "SELECT title AS title FROM poll_titles WHERE
poll_ID=$arr_ac tive[$i]";
$sql_title_resu lt = mysql_query($sq l_title,$conn);
if (!$sql_title_re sult) {
die ("Couldn't execute query : " . mysql_error());
}
$row = mysql_fetch_arr ay($sql_title_r esult);
$title = $row["title"];
if ($title) {
echo "<p><table bgcolor='#FF00A A'><tr><td align='center'
colspan='2'><b> $title</b></td></tr><tr><td><p></p></td></tr>";
// See if user has already voted
$sql_ip = "SELECT COUNT(*) AS count FROM visited_ips WHERE
ip='$REMOTE_ADD R' AND poll_ID=$arr_ac tive[$i]";
$sql_ip_result = mysql_query($sq l_ip,$conn);
if (!$sql_ip_resul t) {
die ("Couldn't execute query : " . mysql_error());
}
$row = mysql_fetch_arr ay($sql_ip_resu lt);
$count = $row["count"];
// Get options and votes
$sql_options = "SELECT options, votes, id, poll_ID FROM poll WHERE
poll_ID=$arr_ac tive[$i]";
$sql_options_re sult = mysql_query($sq l_options,$conn );
if (!$sql_options_ result) {
die ("Couldn't execute query : " . mysql_error());
}
if (($count == 0) && ($_POST["hidden_id"] == $arr_active[$i])) {
/* See which value is checked */
// which poll was submitted?
$cur_poll = $_POST["hidden_id"];
$tmp = "group" . $cur_poll;
$val = $_POST[$tmp];
$sql_update = "UPDATE poll SET votes = votes + 1 WHERE ID = $val";
// execute the query
mysql_query($sq l_update);
// add ip to visited_ips
$sql_visited = "INSERT INTO visited_ips (ip, poll_ID) VALUES
('$REMOTE_ADDR' , $cur_poll)";
mysql_query($sq l_visited);
echo "<tr><td align='left'><u >Option</u></td><td
align='right'>< u>Votes</u></td></tr>";
while ($row = mysql_fetch_arr ay($sql_options _result)) {
$option = $row["options"];
$votes = $row["votes"];
echo "<tr><td align='left'>$o ption: </td><td
align='right'>$ votes</td></tr>";
}
echo "</table></p>";
}
// Display the poll if the user has not yet voted
else if (($count == 0) && ($_POST["hidden_id"] != $arr_active[$i])) {
echo "<form action='pollkit ty.php' method='post'>" ;
while ($row = mysql_fetch_arr ay($sql_options _result)) {
$option = $row["options"];
$id = $row["id"];
$poll_ID = $row["poll_ID"];
$groupname = "group" . $poll_ID;
echo "<tr><td align='left'><i nput type='radio' name='$groupnam e'
value='$id'>$op tion</td></tr>";
}
echo "<tr><td align='right'>
<input type='hidden' name='hidden_id ' value='$poll_ID '>
<input type='Submit' value='submit'> </td></tr>
</form>
</table></p>";
}
// Otherwise, if the user has already voted, display results
else {
echo "<tr><td align='left'><u >Option</u></td><td
align='right'>< u>Votes</u></td></tr>";
while ($row = mysql_fetch_arr ay($sql_options _result)) {
$option = $row["options"];
$votes = $row["votes"];
echo "<tr><td align='left'>$o ption: </td><td
align='right'>$ votes</td></tr>";
}
echo "</table></p>";
}
}
}
echo "</body></html>";
?>
Comment