Poll System
Step One - The database / sql
First we have to create our tables in the database,
open up phpMyAdmin or whatever softwear you use
to edit queries with.
And run the following query onto your database:
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
CREATE TABLE `poll` (
`id` int(11) NOT NULL auto_increment,
`question` varchar(40) NOT NULL default '',
`option1` varchar(30) NOT NULL default '',
`option2` varchar(30) NOT NULL default '',
`option3` varchar(30) NOT NULL default '',
`option4` varchar(30) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=3 ;
CREATE TABLE `pollvotes` (
`id` int(11) NOT NULL auto_increment,
`pollid` varchar(20) NOT NULL default '',
`vote` varchar(20) NOT NULL default '',
`username` varchar(50) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=11 ;
Step Three - newpoll.php
Now lets create a new a file so we can actually make a poll.
Create a new file and name it newpoll.php and insert into it:
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
ob_start();
//allow cookies
require("config.php");
// include config file
if(!isset($_POST[create])){
//if submit button isnt pressed
echo "<form method='post'>
Poll Question:\
<input type='text' name='question' maxlength='40'>\
Option 1:
<input type='text' name='1' maxlength='30'>\
Option 2:
<input type='text' name='2' maxlength='30'>\
Option 3:
<input type='text' name='3' maxlength='30'>\
Option 4:
<input type='text' name='4' maxlength='30'>\
<input type='submit' name='create' value='Submit'></form>";
//echo html form
}
if($_POST[create]){
//if submit button is pressed
$question = $_POST['question'];
//get question field
$op1 = $_POST['1'];
//get option 1 field
$op2 = $_POST['2'];
//get option 2 field
$op3 = $_POST['3'];
//get option 3 field
$op4 = $_POST['4'];
//get option 4 field
if($question == NULL || $op1 == NULL || $op2 == NULL || $op3 == NULL || $op4 == NULL){
// check if any fields are left blank
echo "A field was left blank, please go back and fix this.";
}else{
$addpoll = mysql_query("INSERT INTO poll (question,option1,option2,option3,option4) VALUES
('$question','$op1','$op2','$op3','$op4')");
//insert values
echo "The poll has been created.";
}
}
?>
Step Four - poll.php
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
ob_start();
//allow cookies and header changes
require("config.php");
//include our config file
if(!isset($_POST[vote_poll])){
//if the submit button isnt pushed
$getcurrent = mysql_query("SELECT * FROM poll ORDER BY id DESC LIMIT 1");
//get the current poll info
while($r=mysql_fetch_array($getcurrent)){
$pollid = $r['id'];
if(!isset($_COOKIE['pollvote'])){
//if the cookie isnt set
$voted = "No";
}else{
if($_COOKIE['pollvote'] == $pollid){
$voted = "Yes";
}else{
$voted = "No";
}
}
//poll id
$question = $r['question'];
//poll question
$total = mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid'");
//get poll votes
$total = mysql_num_rows($total);
//get number of poll votes
$option1 = $r['option1'];
//option 1
$option2 = $r['option2'];
//option 2
$option3 = $r['option3'];
//option 3
$option4 = $r['option4'];
//option 4
if($option1 == "na"){
//if option 1 doesnt exist
}else{
$geta = mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid' AND vote='1'");
$numa = mysql_num_rows($geta);
if($numa == 0){
$percenta = "0%";
//bar percent = 0
}else{
$percenta = round(($numa / $total) * 100)."%";
}
}
if($option2 == "na"){
//if option 2 doesnt exist
}else{
$gets = mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid' AND vote='2'");
$nums = mysql_num_rows($gets);
if($nums == 0){
$percents = "0%";
//bar percent = 0
}else{
$percents = round(($nums / $total) * 100)."%";
}
}
if($option3 == "na"){
//if option 3 doesnt exist
}else{
$getd = mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid' AND vote='3'");
$numd = mysql_num_rows($getd);
if($numd == 0){
$percentd = "0%";
//bar percent = 0
}else{
$percentd = round(($numd / $total) * 100)."%";
}
}
if($option4 == "na"){
//if option 4 doesnt exist
}else{
$getf = mysql_query("SELECT * FROM pollvotes WHERE pollid='$pollid' AND vote='4'");
$numf = mysql_num_rows($getf);
if($numf == 0){
$percentf = "0%";
//bar percent = 0
}else{
$percentf = round(($numf / $total) * 100)."%";
}
}
echo "<form method='post'>
<strong>$question</strong>\
";
if($voted == "Yes"){
//if they already voted
echo "$option1($numa)
<img src='poll.gif' height='10'
width='$percenta'>\";//echo option 1
echo "$option2($nums)
<img src='poll.gif' height='10'
width='$percents'>\";//echo option 2
echo "$option3($numd)
<img src='poll.gif' height='10'
width='$percentd'>\";//echo option 3
echo "$option4($numf)
<img src='poll.gif' height='10'
width='$percentf'>\";//echo option 4
}else{
//else
echo "<input type='radio' name='vote' value='1'> $option1\
<input type='radio' name='vote' value='2'> $option2\
<input type='radio' name='vote' value='3'> $option3\
<input type='radio' name='vote' value='4'> $option4\
";
//echo form to vote
}
echo "<input type='submit' value='Submit' name='vote_poll'></form>";
}
}else{
$vote = $_POST['vote'];
//get the vote
$getcurrent = mysql_query("SELECT * FROM poll ORDER BY id DESC LIMIT 1");
//get current poll
while($r=mysql_fetch_array($getcurrent)){
$pollid = $r['id'];
//poll id
}
$voteonpoll = mysql_query("INSERT INTO pollvotes (pollid,vote,username) VALUES
('$pollid','$vote','$Uname')");
//vote
echo "Thank you for voting.";
setcookie("pollvote",$pollid,time()+360000000);
//thanks
}
?>
Thats all, it should be working fine.