Poll for usersystem


Basic poll system, requested by nathan

First off run these in PHPMYADMIN

PHP Code
  1. CREATE TABLE `polls` (
  2. `id` int(11) NOT NULL auto_increment,
  3. `question` varchar(30) NOT NULL default '',
  4. `button1` varchar(30) NOT NULL default '',
  5. `button2` varchar(255) NOT NULL default '',
  6. `button3` varchar(55) NOT NULL default '',
  7. `button4` varchar(55) NOT NULL default '',
  8. PRIMARY KEY (`id`)
  9. ) TYPE=MyISAM;
  10.  
  11. CREATE TABLE `pollvotes` (
  12. `id` int(11) NOT NULL auto_increment,
  13. `vote` varchar(30) NOT NULL default '',
  14. `username` varchar(30) NOT NULL default '',
  15. PRIMARY KEY (`id`)
  16. ) TYPE=MyISAM;


Now, read this and name it poll.php

PHP Code
  1. <?
  2. /////////////////////////////////
  3. // poll.php by new2old //
  4. // from rmb-scripting.com //
  5. /////////////////////////////////
  6.  
  7. // DO NOT REMOVE THE ABOVE
  8.  
  9. session_start(); //starts sessions
  10. include ( "config.php" ); //includes config
  11.  
  12. if ($logged['username']) {
  13. if (!$_POST['pollsubmit']) { //checks if poll was subbmitted
  14.  
  15. $select = mysql_query(mysql_fetch_array( "SELECT * FROM `poll`" )); //gets poll detales
  16.  
  17. echo ( "<form method='POST'>" );
  18. echo ( "$select[quesion]" );
  19. echo ( "<input type='radio' name='button1' value='$select[button1]'> $select[button1]" ); // shows option 1
  20. echo ( "<input type='radio' name='button2' value='$select[button2]'> $select[button2]" ); // shows option 2
  21. echo ( "<input type='radio' name='button3' value='$select[button3]'> $select[button3]" ); // shows option 3
  22. echo ( "<input type='radio' name='button4' value='$select[button4]'> $select[button4]" ); // shows option 4
  23. echo ( "<input type='submit' name='pollsubmit' value='Vote'>" ); // shows the vote button
  24.  
  25. } elseif ($_POST['pollsubmit']) { // else it has been posted
  26.  
  27. $op1 = $_POST['button1']; // get posted info
  28. $op2 = $_POST['button2']; // get posted info
  29. $op3 = $_POST['button3']; // get posted info
  30. $op4 = $_POST['button4']; // get posted info
  31.  
  32. if ($_POST['$op1'] == "1") { //if you voted option1
  33.  
  34. $do = mysql_query( "INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op1' , '" . $logged['username'] . "'));
  35. echo ( "You voted for $op1" );
  36.  
  37. } elseif ($_POST['$op2'] == "1") { //or if you voted option2
  38.  
  39. $do = mysql_query( "INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op2' , '" . $logged['username'] . "'));
  40. echo ( "You voted for $op2" );
  41.  
  42. } elseif ($_POST['$op3'] == "1") { //or if you voted option3
  43.  
  44. $do = mysql_query( "INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op3' , '" . $logged['username'] . "'));
  45. echo ( "You voted for $op3" );
  46.  
  47. } elseif ($_POST['$op4'] == "1") { //or if you voted option4
  48.  
  49. $do = mysql_query( "INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op4' , '" . $logged['username'] . "'));
  50. echo ( "You voted for $op1" );
  51.  
  52. }
  53. }
  54. } else {
  55.  
  56. echo ( "Sign in to vote on the poll" );
  57.  
  58. }
  59.  
  60. ?>


Where you want that in your template simply add

PHP Code
  1.  
  2. <? include ( "poll.php" ); ?>


Now call this adminpoll.php

PHP Code
  1. <?
  2. //////////////////////////////
  3. // adminpoll.php by new2old //
  4. // for rmb-scripting.com //
  5. //////////////////////////////
  6.  
  7. // DO NOT REMOVE THE ABOVE
  8.  
  9. session_start(); // starts session
  10. include ( "config.php" ); // gets config
  11.  
  12. if ($logged['username'] && $logged['userlevel'] == "6") { // checks if user is logged in and an admin
  13.  
  14. switch($_GET['delete']) { //makes multiple pages
  15.  
  16. default:
  17.  
  18. if (!$_POST['addpoll']) {
  19.  
  20. $check = mysql_query(mysql_fetch_array( "SELECT * FROM `polls`" ));
  21.  
  22. $checks = mysql_num_rows($check); // counts for polls
  23.  
  24. if ($checks == "1") { //haha sorry one poll at a time
  25.  
  26. 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 :)
  27.  
  28. } elseif ($checks == "0") { //checking there are no current polls
  29.  
  30. echo ( "Enter the poll values below" );
  31. echo ( "<form method='POST'>" ); // starts form
  32. echo ( "<input tpye='text' name='question'>" ); //question
  33. echo ( "<input type='text' name='button1'>" ); //option 1 :)
  34. echo ( "<input type='text' name='button2'>" ); //option 2 :)
  35. echo ( "<input type='text' name='button3'>" ); //option 3 :)
  36. echo ( "<input type='text' name='button4'>" ); //option 4 :)
  37. echo ( "<input type='submit' name='addpoll' value='Add'>" ); //add the poll :)
  38.  
  39. }
  40.  
  41. } elseif ($_POST['addpoll']) { // or if form has been submitted
  42.  
  43. $op1 = $_POST['button1']; // get posted info
  44. $op2 = $_POST['button2']; // get posted info
  45. $op3 = $_POST['button3']; // get posted info
  46. $op4 = $_POST['button4']; // get posted info
  47.  
  48. $do = mysql_query( "INSERT INTO `polls` ( `question` , `button1` , `button2` , `button3` , `button4`) VALUES ('$ques' , '$op1' , '$op2' , '$op3' , '$op4');
  49. echo ( "Poll Added" ); // poll is added
  50.  
  51. }
  52. break; // end of default page
  53.  
  54. case 'do': //new page 'do'
  55.  
  56. $do = mysql_query( "DELETE * FROM `polls` WHERE `id` = '" . $_GET['id'] . "');
  57. echo ( "Poll Deleted" ); //deleted poll
  58.  
  59. break; // end page 'do'
  60.  
  61. } // end switch
  62. } else {
  63.  
  64. echo ( "You are either not logged in or are not an Admin" ); // not logged in or are not an admin
  65.  
  66. }
  67.  
  68. ?>


Thats it :)

any errors please let me no

Thanks
new2old's Avatar
Author:
Views:
4,568
Rating:
Posted on Wednesday 27th August 2008 at 08:27 PM
new2old
new2old's Avatar
Urban replace

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


with

PHP Code
  1.  
  2. $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's Avatar
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's Avatar
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's Avatar
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's Avatar
poll.php

fixed :)

PHP Code
  1. <?
  2. /////////////////////////////////
  3. // poll.php by new2old //
  4. // from rmb-scripting.com //
  5. /////////////////////////////////
  6.  
  7. // DO NOT REMOVE THE ABOVE
  8.  
  9. session_start(); //starts sessions
  10. include ( "config.php" ); //includes config
  11.  
  12. if ($logged['username']) {
  13. if (!$_POST['pollsubmit']) { //checks if poll was subbmitted
  14.  
  15. $select = mysql_query(mysql_fetch_array( "SELECT * FROM `poll`" )); //gets poll detales
  16.  
  17. echo ( "<form method='POST'>" );
  18. echo ( "$select[quesion]" );
  19. echo ( "<input type='radio' name='button1' value='$select[button1]'> $select[button1]" ); // shows option 1
  20. echo ( "<input type='radio' name='button2' value='$select[button2]'> $select[button2]" ); // shows option 2
  21. echo ( "<input type='radio' name='button3' value='$select[button3]'> $select[button3]" ); // shows option 3
  22. echo ( "<input type='radio' name='button4' value='$select[button4]'> $select[button4]" ); // shows option 4
  23. echo ( "<input type='submit' name='pollsubmit' value='Vote'>" ); // shows the vote button
  24.  
  25. } elseif ($_POST['pollsubmit']) { // else it has been posted
  26.  
  27. $op1 = $_POST['button1']; // get posted info
  28. $op2 = $_POST['button2']; // get posted info
  29. $op3 = $_POST['button3']; // get posted info
  30. $op4 = $_POST['button4']; // get posted info
  31.  
  32. if ($_POST['$op1'] == "1") { //if you voted option1
  33.  
  34. $do = mysql_query( "INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op1' , '" . $logged['username'] . "') ");
  35. echo ( "You voted for $op1" );
  36.  
  37. } elseif ($_POST['$op2'] == "1") { //or if you voted option2
  38.  
  39. $do = mysql_query( "INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op2' , '" . $logged['username'] . "') ");
  40. echo ( "You voted for $op2" );
  41.  
  42. } elseif ($_POST['$op3'] == "1") { //or if you voted option3
  43.  
  44. $do = mysql_query( "INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op3' , '" . $logged['username'] . "') ");
  45. echo ( "You voted for $op3" );
  46.  
  47. } elseif ($_POST['$op4'] == "1") { //or if you voted option4
  48.  
  49. $do = mysql_query( "INSERT INTO `pollvotes` (`vote` , `username`) VALUES ('$op4' , '" . $logged['username'] . "') ");
  50. echo ( "You voted for $op1" );
  51.  
  52. }
  53. }
  54. } else {
  55.  
  56. echo ( "Sign in to vote on the poll" );
  57.  
  58. }
  59.  
  60. ?>


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

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


unless you took out

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


which it says not to
Posted on Saturday 19th April 2008 at 12:51 PM
cyruswu
cyruswu's Avatar
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's Avatar
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's Avatar
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's Avatar
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.