Tutorial: How to build a poll system with PHP and mySQL

Tutorial: How to build a poll system with PHP and mySQL

4. Now that we have our database connection, lets make another file. Call it poll.php. Seeing as though the poll system won’t be diverse in terms of functionality, we really only need one file to handle the requests, and one file to handle the design. I could of put both of these together, but it is easier to style and read if you have your HTML separate from your core PHP code. So lets make the other file now too, make a new file called index.php and open up index.php

For now lets just put this code in:


All this does is join the config.php file with the index.php file so to speak. So when you use an include command, you include all the functions, all the output, all the receivers into the other file, as if they were always one file. This is a handy tool to segregate your code, which means less double up on code writing.

5. Next open up the poll.php file and lets add some database functions. We need to select the most recent poll question and all of its details. Also we need to find out if the visitor has already submitted their opinion for this poll question. Don’t worry i will explain the logic behind the PHP code after you put it in:

  $query = mysql_query("SELECT * FROM `poll` ORDER BY `id` DESC LIMIT 1");
	$rows = mysql_num_rows($query);

	if($rows > 0){
		$poll = mysql_fetch_array($query);
		$title = $poll['name'];
	} else {
		$title = 'No Poll Yet';
	}

So what we have here is a query that checks the database for any questions. The first line $query = mysql_query(“SELECT * FROM `poll` ORDER BY `id` LIMIT 1″); is a mysql query. It says: look in the table that is called poll, order all of the data in this table by their id’s, then (LIMIT 1) select 1 row. The id’s in this table are assigned automatically, it is a mysql thing, called auto_increment. So the newest item, in this tutorial’s case, will have the largest id. Simple. For other more advanced systems you might need to use timestamps, but since this is a light weight system that would be over kill.

The second line $rows = mysql_num_rows($query); looks at the previous query and looks for how many rows were returned. Now you might be thinking this is quite stupid, we told it to return 1 row. Right, we did. But what happens if their are no questions in the database, i.e. you have no polls set up. Well then it would return 0. The following code then tests if the $rows variable is set to 0 and then handles the display of the title accordingly.

If the $rows variable is over 0 it will execute this code:

$poll = mysql_fetch_array($query);
		$title = $poll['name'];

Which basically means, look at the query above, put all that data into an array format, and store it in the variable $poll. If you don’t know what an array is, think of excel. Its like a spreadsheet. We get information out of our spreadsheet by names, in this case our column identifier would be ‘name’ because that’s what’s in our database. So to get the name value out, and in turn the title of our current poll, we use this format: $poll['name']; Excellent.

The following code is html, which shows the title. If the number of rows was 0, i.e. no questions, it would display: ‘No Poll Yet’. If it was over 0, it will display the title of the poll that is most recent (has the highest id). Lets keep on truckin’

Posted by VoiDeT

Categorised under PHP
Bookmark the permalink or leave a trackback.

70 Comments

  1. voidet. Of course! Sent you some cash and put a link in.
    Many thanks for your hard work!

    July 31, 2009 @ 12:03 am
  2. VoiDeT

    Hey!
    Thanks alot for the donation!
    I really appreciate that! If people are using the tutorial as a learning tool i’m all for it, we both can build something up that way. But if people are going to use it for their own web apps, it’s not right to just take and not give anything back :)

    But thanks alot for the donation! I am glad it worked out for you :D
    Also if you need any future help with anything just let me know!

    July 31, 2009 @ 10:29 am
  3. Honestly it was a learning tool for me. And I ended up using it successfully which I thought was very cool.
    I am happy to give back and may call on you again. Thanks very much.

    July 31, 2009 @ 10:57 am
  4. VoiDeT

    Hey Artech!

    Really cool portfolio you have!
    Love the graphics!

    July 31, 2009 @ 11:41 am
  5. Artech

    Thanks Voidet.
    I enjoy working with flash. Creating with graphics in mind is usually how I approach things. But I’m getting more and more into the scripting side of things. Really appreciating the functionality and management.

    July 31, 2009 @ 1:48 pm
  6. VoiDeT

    You should get into the frameworks!
    Just jump into CakePHP :D

    July 31, 2009 @ 2:02 pm
  7. frank

    will this code work if i want to ask a series of independant yes or no question that i want to rate separately?

    August 24, 2009 @ 11:57 pm
  8. VoiDeT

    This is a poll script.
    To turn it into a survey type program would require some refactoring.
    Not too much if you want it hardcoded, but alot if you want it to be dynamic :)

    August 24, 2009 @ 11:59 pm
  9. Hi, great tutorial, thanks man. :D

    August 25, 2009 @ 4:10 am
  10. iz

    hi, anyone know how we get the value of $responses['total'] hmm i starter in php wondering where we get the ‘total’ from? thx for any help..

    August 27, 2009 @ 11:42 am
  11. VoiDeT

    IZ please clarify here!
    Do an echo $responses['total'] to output the total number of responses!

    August 27, 2009 @ 11:54 pm
  12. Diogo

    Really good tutorial!! THX!! I’m just modifying a bit to fit on my page and to my taste.

    But again, thx, i learned a lot here!

    October 14, 2009 @ 8:42 am
  13. Hi.. nice tutoria! i have a question

    Warning: Wrong parameter count for max() in C:\wamp\www\poll\poll.php on line 17

    what does it mean? and how can i solve it? thanks…

    January 11, 2010 @ 3:33 pm
  14. VoiDeT

    Hey Ralph,

    Below “$query = mysql_query(“SELECT COUNT(`id`) as hits FROM `responses` GROUP BY `qid`”);”
    Add:

    $me = array();

    January 15, 2010 @ 9:22 am
  15. Ralph

    thanks VoiDeT.. ill try it! ^__^

    January 21, 2010 @ 12:19 am
  16. shaeeb

    Hi, followed your tutorial, this is for personal learning.. im new to PHP. was a great tutorial even for those who don’t code all the time. I just had 2 questions, everything is up and working, but my main page is also called index.php, if i change the name of the index.php that you set as default would it still work fine?

    also, how do i add more polls? i want to add atleast three or four questions, thanks.

    March 23, 2010 @ 2:00 am
  17. VoiDeT

    Try changing the name and see what happens.
    There might be references to index.php in the code, or it could just be SELF. I forget a lot of the code in this, as this desperately needs a re-write.
    If you want to add more polls or questions you should do so in the database. However you will need to cycle through the polls somehow.

    Thanks,
    Richard

    March 23, 2010 @ 8:49 am
  18. Shaeeb

    Thanks, I will try changing the name and see what happens, I basically want to have 3-4 polls running at the same time, if I cant sort it out I will have to stick to one poll

    March 23, 2010 @ 11:10 pm
  19. Richard

    Hi Voidet,

    I clicked on the link you give above but it seems to be malfunctioning…http://www.jotlab.com/tutorials/poll/index.php
    ++++++++++++++++++++++++++++++++++++++++++++++++Warning: mysql_connect() [function.mysql-connect]: Access denied for user ‘voidet_jotlab’@'localhost’ (using password: YES) in /var/www/jotlab.com/public_html/tutorials/poll/config.php on line 10

    Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user ‘www-data’@'localhost’ (using password: NO) in /var/www/jotlab.com/public_html/tutorials/poll/config.php on line 11

    Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /var/www/jotlab.com/public_html/tutorials/poll/config.php on line 11

    Warning: mysql_query() [function.mysql-query]: Access denied for user ‘www-data’@'localhost’ (using password: NO) in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 3

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 3

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 4

    Warning: mysql_query() [function.mysql-query]: Access denied for user ‘www-data’@'localhost’ (using password: NO) in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 13

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 13

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 14

    Warning: Wrong parameter count for max() in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 17

    Warning: mysql_query() [function.mysql-query]: Access denied for user ‘www-data’@'localhost’ (using password: NO) in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 19

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 19

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 21

    Warning: mysql_query() [function.mysql-query]: Access denied for user ‘www-data’@'localhost’ (using password: NO) in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 77

    Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 77

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/jotlab.com/public_html/tutorials/poll/poll.php on line 78
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    any help is always appreciated
    rca08207 at bigpond dot net dot au

    April 6, 2010 @ 5:30 pm
  20. A very detailed and helpful tutorial indeed. Loved it. I made a collection of all such tutorials and scripts (including this one) at 11 PHP Voting Scripts and Tutorials. I hope it would be helpful to many. :-)

    June 13, 2010 @ 9:10 pm

3 Trackbacks

  1. [...] 7.How to build a poll system with PHP and mySQL : [...]

  2. [...] Read the original post: Tutorial: How to build a poll system with PHP and mySQL • Jotlab [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

or