0% found this document useful (0 votes)
207 views

PHP Poll

The document describes a simple PHP-based polling system with two files and two database tables. The system allows users to view poll questions and vote on options without storing IP addresses to allow multiple votes from the same address. Administrators can create new polls by adding rows to the poll_main database table and defining poll options in poll_choice.

Uploaded by

xunrage
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
207 views

PHP Poll

The document describes a simple PHP-based polling system with two files and two database tables. The system allows users to view poll questions and vote on options without storing IP addresses to allow multiple votes from the same address. Administrators can create new polls by adding rows to the poll_main database table and defining poll options in poll_choice.

Uploaded by

xunrage
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

The following code will not keep IPs inside the database.

Therefore it will not try to prevent multiple


votes from the same address. In fact it tries to be as plain and bare bone as it can. It consists of two files
placed inside the same folder and two tables placed in the same database.

----- access.php -----------------------------------------------------


<?php
define('DB_HOST','your_host');
define('DB_USER','your_user');
define('DB_PASS','your_password');
define('DB_NAME','your_database');
$dbhandler=@mysql_connect(DB_HOST,DB_USER,DB_PASS) or
die('Unexpected error: '.mysql_error());
mysql_select_db(DB_NAME) or die('Access rejected: '.mysql_error());
?>
----------------------------------------------------------------------

----- poll.php -------------------------------------------------------


<?php
require_once("./access.php");

// sanitization of input
function escape_data($data){
global $dbhandler;
$data=htmlentities($data);
if(ini_get('magic_quotes_gpc')){
$data=stripslashes($data);
}
return mysql_real_escape_string(trim($data),$dbhandler);
}

// poll in use
$activepollid="1";

// show results
if(isset($_GET['showresults'])){
$usershow=escape_data($_GET['showresults']);
if($usershow=="on"){
$pollinfo=mysql_fetch_array(mysql_query("SELECT * FROM poll_main WHERE
id='$activepollid'"),MYSQL_ASSOC);
echo "<h3>$pollinfo[question]</h3>
<div><table cellpadding=\"5\" cellspacing=\"0\" border=\"1\"><caption>Results</caption>
<tr><th>Options</th><th>Votes</th></tr>";

$choicedata=mysql_query("SELECT * FROM poll_choice WHERE pollid='$activepollid' ORDER BY


id ASC");
$totalvotes=0;
while($choiceinfo=mysql_fetch_array($choicedata,MYSQL_ASSOC)){
$totalvotes=$totalvotes+$choiceinfo["votes"];
}
mysql_data_seek($choicedata,0);
while($choiceinfo=mysql_fetch_array($choicedata,MYSQL_ASSOC)){
echo "<tr><td>$choiceinfo[text]</td>";
if($totalvotes!=0)
$percent=number_format(round($choiceinfo["votes"]*100/$totalvotes,2),2, ".","");
else $percent="n/a";
echo "<td>$choiceinfo[votes] votes($percent %)</td></tr>";
}

echo "</table></div>";
echo "<p><a href='".$_SERVER['PHP_SELF']."'>Go Back</a></p>";

1
exit(0);
}
}

// submit vote
if(isset($_POST['choice'])){
$userchoice=escape_data($_POST['choice']);
if(!mysql_query("UPDATE poll_choice SET votes=votes+1 WHERE pollid='$activepollid' AND
id='$userchoice'")){
echo "<p>Error: Vote was not commited.</p>";
} else {
echo "<p><b>Thank you!</b></p>";
}
echo "<p><a href='?showresults=on'>See Results</a></p>";
echo "<p><a href='".$_SERVER['PHP_SELF']."'>Go Back</a></p>";
exit(0);
}

// show poll
$pollinfo=mysql_fetch_array(mysql_query("SELECT * FROM poll_main WHERE
id='$activepollid'"),MYSQL_ASSOC);
$form=$_SERVER['PHP_SELF'];
echo "<h3>$pollinfo[question]</h3>
<div><form method='post' action='$form'>";
$choicedata=mysql_query("SELECT * FROM poll_choice WHERE pollid='$activepollid' ORDER BY id
ASC");
while($choiceinfo=mysql_fetch_array($choicedata,MYSQL_ASSOC)){
echo "<input type='radio' name='choice' value='$choiceinfo[id]'
id='choice".$choiceinfo["id"]."'/><label
for='choice".$choiceinfo["id"]."'>$choiceinfo[text]</label><br />";
}
echo "<input type='submit' value='Cast Your Vote' class='button' /></form>";
echo "<p><a href='?showresults=on'>See Results</a></p></div>";
?>
----------------------------------------------------------------------

Database tables are as follows:

--
-- Table structure for table `poll_main`
--

CREATE TABLE `poll_main` (


`id` int(11) NOT NULL auto_increment,
`question` varchar(200) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `poll_main`
--

INSERT INTO `poll_main` VALUES (1, 'Question 1?');


INSERT INTO `poll_main` VALUES (2, 'Question 2?');

--
-- Table structure for table `poll_choice`
--

CREATE TABLE `poll_choice` (


`id` int(11) NOT NULL auto_increment,
`pollid` int(11) NOT NULL default '0',

2
`text` varchar(200) NOT NULL default '',
`votes` int(11) NOT NULL default '0',
PRIMARY KEY (`id`,`pollid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

--
-- Dumping data for table `poll_choice`
--

INSERT INTO `poll_choice` VALUES (1, 1, 'Poll_One_Option_1', 0);


INSERT INTO `poll_choice` VALUES (2, 1, 'Poll_One_Option_2', 0);
INSERT INTO `poll_choice` VALUES (3, 1, 'Poll_One_Option_3’, 0);
INSERT INTO `poll_choice` VALUES (4, 2, 'Poll_Two_Option_1', 0);
INSERT INTO `poll_choice` VALUES (5, 2, 'Poll_Two_Option_2', 0);

The nice thing about this poll is that you can have as many options as you like. Just insert the appropriate
number of rows into poll_choice table. You can retain old polls inside the database and create new ones
by adding them to poll_main table. After you create a new poll you have to change only the value for the
variable $activepollid at the beginning of the script and your poll is ready to go public.

You might also like