Php Poll

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Php Poll as PDF for free.

More details

  • Words: 459
  • Pages: 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 ----------------------------------------------------

-------------------------------------------------------------------------- poll.php ------------------------------------------------------$pollinfo[question]
"; $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 ""; if($totalvotes!=0) $percent=number_format(round($choiceinfo["votes"]*100/$totalvotes,2),2, ".",""); else $percent="n/a"; echo ""; } echo "
Results
OptionsVotes
$choiceinfo[text]$choiceinfo[votes] votes($percent %)
"; echo "

Go Back

";

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 "

Error: Vote was not commited.

"; } else { echo "

Thank you!

"; } echo "

See Results

"; echo "

Go Back

"; exit(0); } // show poll $pollinfo=mysql_fetch_array(mysql_query("SELECT * FROM poll_main WHERE id='$activepollid'"),MYSQL_ASSOC); $form=$_SERVER['PHP_SELF']; echo "

$pollinfo[question]

"; $choicedata=mysql_query("SELECT * FROM poll_choice WHERE pollid='$activepollid' ORDER BY id ASC"); while($choiceinfo=mysql_fetch_array($choicedata,MYSQL_ASSOC)){ echo "
"; } echo "
"; echo "

See Results

"; ?>

---------------------------------------------------------------------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 INSERT INSERT INSERT INSERT

INTO INTO INTO INTO INTO

`poll_choice` `poll_choice` `poll_choice` `poll_choice` `poll_choice`

VALUES VALUES VALUES VALUES VALUES

(1, (2, (3, (4, (5,

1, 1, 1, 2, 2,

'Poll_One_Option_1', 'Poll_One_Option_2', 'Poll_One_Option_3’, 'Poll_Two_Option_1', 'Poll_Two_Option_2',

0); 0); 0); 0); 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.

3

Related Documents

Php Poll
May 2020 3
Php
November 2019 9
Php
October 2019 10
Php
November 2019 13
Php
June 2020 22
Php
November 2019 46