Forgot Password / Register
Site Statistics
Total Members: 520
Total Tutorials: 242
Newsest User: 8884244477
Todays Unique Hits: 661
1 Users 5 Guests Online

Poll for usersystem

Basic poll system, requested by nathan

First off run these in PHPMYADMIN

Code

CREATE TABLE `polls` (
`id` int(11) NOT NULL auto_increment,
`question` varchar(30) NOT NULL default '',
`button1` varchar(30) NOT NULL default '',
`button2` varchar(255) NOT NULL default '',
`button3` varchar(55) NOT NULL default '',
`button4` varchar(55) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;

CREATE TABLE `pollvotes` (
`id` int(11) NOT NULL auto_increment,
`vote` varchar(30) NOT NULL default '',
`username` varchar(30) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;


Now, read this and name it poll.php

Code

<?
/////////////////////////////////
// poll.php by new2old //
// from rmb-scripting.com //
/////////////////////////////////

// DO NOT REMOVE THE ABOVE

session_start(); //starts sessions
include ( "config.php" ); //includes config

if ($logged['username']) {
if (!$_POST['pollsubmit']) { //checks if poll was subbmitted

$select = mysql_query(mysql_fetch_array( "SELECT * FROM `poll`" )); //gets poll detales

echo ( "<form method='POST'>" );
echo ( "$select[quesion]" );
echo ( "<input type='radio' name='button1' value='$select[button1]'> $select[button1]" ); // shows option 1
echo ( "<input type='radio' name='button2' value='$select[button2]'> $select[button2]" ); // shows option 2
echo ( "<input type='radio' name='button3' value='$select[button3]'> $select[button3]" ); // shows option 3
echo ( "<input type='radio' name='button4' value='$select[button4]'> $select[button4]" ); // shows option 4
echo ( "<input type='submit' name='pollsubmit' value='Vote'>" ); // shows the vote button

} elseif ($_POST['pollsubmit']) { // else it has been posted

$op1 = $_POST['button1']; // get posted info
$op2 = $_POST['button2']; // get posted info
$op3 = $_POST['button3']; // get posted info
$op4 = $_POST['button4']; // get posted info

if ($_POST['$op1'] == "1") { //if you voted option1

$do = mysql_query( "INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op1' , '" . $logged['username'] . "'));
echo ( "You voted for $op1" );

} elseif ($_POST['$op2'] == "1") { //or if you voted option2

$do = mysql_query( "INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op2' , '" . $logged['username'] . "'));
echo ( "You voted for $op2" );

} elseif ($_POST['$op3'] == "1") { //or if you voted option3

$do = mysql_query( "INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op3' , '" . $logged['username'] . "'));
echo ( "You voted for $op3" );

} elseif ($_POST['$op4'] == "1") { //or if you voted option4

$do = mysql_query( "INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op4' , '" . $logged['username'] . "'));
echo ( "You voted for $op1" );

}
}
} else {

echo ( "Sign in to vote on the poll" );

}

?>


Where you want that in your template simply add

Code


<? include ( "poll.php" ); ?>



Now call this adminpoll.php

Code

<?
//////////////////////////////
// adminpoll.php by new2old //
// for rmb-scripting.com //
//////////////////////////////

// DO NOT REMOVE THE ABOVE

session_start(); // starts session
include ( "config.php" ); // gets config

if ($logged['username'] && $logged['userlevel'] == "6") { // checks if user is logged in and an admin

switch($_GET['delete']) { //makes multiple pages

default:

if (!$_POST['addpoll']) {

$check = mysql_query(mysql_fetch_array( "SELECT * FROM `polls`" ));

$checks = mysql_num_rows($check); // counts for polls

if ($checks == "1") { //haha sorry one poll at a time

echo ( "You can only have one poll at a time, please delete the current one by clicking <a href='adminpoll.php?delete=do&id=check[id]'>" ); // delete old poll first :)

} elseif ($checks == "0") { //checking there are no current polls

echo ( "Enter the poll values below" );
echo ( "<form method='POST'>" ); // starts form
echo ( "<input tpye='text' name='question'>" ); //question
echo ( "<input type='text' name='button1'>" ); //option 1 :)
echo ( "<input type='text' name='button2'>" ); //option 2 :)
echo ( "<input type='text' name='button3'>" ); //option 3 :)
echo ( "<input type='text' name='button4'>" ); //option 4 :)
echo ( "<input type='submit' name='addpoll' value='Add'>" ); //add the poll :)

}

} elseif ($_POST['addpoll']) { // or if form has been submitted

$op1 = $_POST['button1']; // get posted info
$op2 = $_POST['button2']; // get posted info
$op3 = $_POST['button3']; // get posted info
$op4 = $_POST['button4']; // get posted info

$do = mysql_query( "INSERT INTO `polls` ( `question` , `button1` , `button2` , `button3` , `button4`) VALUES ('$ques' , '$op1' , '$op2' , '$op3' , '$op4');
echo ( "Poll Added" ); // poll is added

}
break; // end of default page

case 'do': //new page 'do'

$do = mysql_query( "DELETE * FROM `polls` WHERE `id` = '" . $_GET['id'] . "');
echo ( "Poll Deleted" ); //deleted poll

break; // end page 'do'

} // end switch
} else {

echo ( "You are either not logged in or are not an Admin" ); // not logged in or are not an admin

}

?>


Thats it :)

any errors please let me no

Thanks
new2old
Author:
Views:
3113
Rating:
Posted on Wednesday 27th August 2008 at 08:27 PM
new2old
new2old
Urban replace

PHP Code
1
2
3
4
$select = mysql_query(mysql_fetch_array( "SELECT * FROM `poll`" )); //gets poll detales


with

PHP Code
1
2
3
4
$select = mysql_query(mysql_fetch_array( "SELECT * FROM `polls`" )); //gets poll detales


thanks
Posted on Monday 4th August 2008 at 02:33 AM
UrbanTwitch
UrbanTwitch
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/jsfdan/public_html/poll.php on line 15
Posted on Friday 25th April 2008 at 03:12 PM
Dalez
Dalez
Poll_admin:

Parse error: syntax error, unexpected '[' in /home/www/zudan.awardspace.com/poll_admin.php on line 18

:S But you need it?
Posted on Monday 21st April 2008 at 10:59 AM
test
test
u missed somethin

$select = mysql_query(mysql_fetch_array( "SELECT * FROM `poll`" )); //gets poll detales

should be

$select = mysql_query(mysql_fetch_array( "SELECT * FROM `polls`" )); //gets poll detales
Posted on Saturday 19th April 2008 at 02:21 PM
new2old
new2old
poll.php

fixed :)

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
<?
/////////////////////////////////
// poll.php by new2old //
// from rmb-scripting.com //
/////////////////////////////////

// DO NOT REMOVE THE ABOVE

session_start(); //starts sessions
include ( "config.php" ); //includes config

if ($logged['username']) {
if (!
$_POST['pollsubmit']) { //checks if poll was subbmitted

$select mysql_query(mysql_fetch_array"SELECT * FROM `poll`" )); //gets poll detales

echo ( "<form method='POST'>" );
echo ( 
"$select[quesion]);
echo ( 
"<input type='radio' name='button1' value='$select[button1]'> $select[button1]); // shows option 1
echo ( "<input type='radio' name='button2' value='$select[button2]'> $select[button2]); // shows option 2
echo ( "<input type='radio' name='button3' value='$select[button3]'> $select[button3]); // shows option 3
echo ( "<input type='radio' name='button4' value='$select[button4]'> $select[button4]); // shows option 4
echo ( "<input type='submit' name='pollsubmit' value='Vote'>" ); // shows the vote button

} elseif ($_POST['pollsubmit']) { // else it has been posted

$op1 $_POST['button1']; // get posted info
$op2 $_POST['button2']; // get posted info
$op3 $_POST['button3']; // get posted info
$op4 $_POST['button4']; // get posted info

if ($_POST['$op1'] == "1") { //if you voted option1

$do mysql_query"INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op1' , '" $logged['username'] . "') ");
echo ( 
"You voted for $op1);

} elseif (
$_POST['$op2'] == "1") { //or if you voted option2

$do mysql_query"INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op2' , '" $logged['username'] . "') ");
echo ( 
"You voted for $op2);

} elseif (
$_POST['$op3'] == "1") { //or if you voted option3

$do mysql_query"INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op3' , '" $logged['username'] . "') ");
echo ( 
"You voted for $op3);

} elseif (
$_POST['$op4'] == "1") { //or if you voted option4

$do mysql_query"INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op4' , '" $logged['username'] . "') ");
echo ( 
"You voted for $op1);

}
}
} else {

echo ( 
"Sign in to vote on the poll" );

}

?>


Please leave the text in that shows i made it thanks
Posted on Saturday 19th April 2008 at 02:16 PM
new2old
new2old
Dalez line 10 is

PHP Code
1
2
include ( "config.php" ); // gets config


unless you took out

PHP Code
1
2
3
4
5
//////////////////////////////
// adminpoll.php by new2old //
// for rmb-scripting.com //
//////////////////////////////


which it says not to
Posted on Saturday 19th April 2008 at 12:51 PM
cyruswu
cyruswu
This looks well done. Haven't tested it or anything to it yet but just reading it it's quite organized.
Posted on Saturday 19th April 2008 at 11:30 AM
Dalez
Dalez
Parse error: syntax error, unexpected '[' in /home/a8067842/public_html/zudan/community/admin_poll.php on line 10

In admin poll :(
Posted on Saturday 19th April 2008 at 11:27 AM
Dalez
Dalez
Also, the problem doesn't seem to be the $do.
Line 35 on myne is echo ("You voted for $op1");

:S
Posted on Saturday 19th April 2008 at 11:25 AM
Dalez
Dalez
Parse error: syntax error, unexpected T_STRING in /home/a8067842/public_html/zudan/community/poll.phpon line 35

I tried to edit it with what you said ^^ But it didn't work.