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:
-
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" />
-
<?php
-
include('config.php');
-
include('poll.php');
-
?>
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:
-
-
if($rows > 0){
-
$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:
-
$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'

17 Responses to “Tutorial: How to build a poll system with PHP and mySQL”
By Cristi
on Jun 11, 2008
It would be perfect if it had a form to add the question and a form to edit them. Anyway Good Job.
By VoiDeT
on Jun 11, 2008
Yep sure.
You should take the guestbook tutorial and build that feature yourself! It is not hard at all
By chor khee
on Jul 16, 2008
nice one man! just a simple question. Can this be done using jsp and mYsql? are there any links for that?
By VoiDeT
on Jul 16, 2008
Hey Chor Khee,
I don’t know JSP, sorry.
Have fun with the port!
By Ana
on Jul 25, 2008
Thank you very much it really is great, it gave me the whole picture. I am using Access data base, how can I use this nice piece of code for Access. Thanks again.
By Hunter
on Aug 11, 2008
Hi,
I downloaded the source, and uploaded it in tact, minus editing in my database information.
I get this error:
http://www.nineprinces.org/hunter/php/tutorials/
”
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/backline/public_html/hunter/php/tutorials/poll.php on line 4″
What’s up w/ that?
By Hunter
on Aug 11, 2008
Nm, fixed it
By VoiDeT
on Aug 11, 2008
And how did you fix it?
By Hanayo
on Sep 24, 2008
I tried working on it but I as totally lost when it came to the whole phpmyAdmin thing??
I couldn’t figure out what you were talking about the click Insert or whatever since I didn’t see that anywhere on my page…
Like I know how to read html/css fairly well and can read xhtml. But php is what I’m learning, and so i know i put the codes in all the right spots but I don’t know what you meant by that and I tried but I dont’ think i got it…
http://www.aestheticloveinc.com/poll/index.php and it’s just blank….
By blckheart
on Sep 25, 2008
RE: And how did you fix it? Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in
To fix the error, you will have to connect to the database. In the downloaded files there is no config.php file or db.inc file to connect to the database, therefore the mysql_num_rows cannot find any info from the database, because there is no connection to the database. Or at least this is what happened in my case.
To connect to the database use the following code in the poll.php file or create a seperate config.php file and just use an include ‘config.php’; or require ‘config.php’; in the top of your poll.php file
//Connection to Database
$hostName = “localhost”;
$databaseName = “yourdatabasename”;
$databaseusername = “yourdbusername”;
$dbpassword = “yourdbpassword”;
$connect = @mysql_connect($host, $yourdbusername, $yourdbpassword);
if (!mysql_select_db($databaseName, $connect))
echo ‘Could not connect to database’;
I hope this helps, i dont know why someone would ask for help on the error and then say he fixed it without giving a solution, he has the time to ask but not the time to help, i think that sucks!
By VoiDeT
on Sep 27, 2008
blckheart what do you think is sitting in the root folder? would it be a config.php file by anychance? With this code in it?
< ?php
//database settings
$hostname = 'localhost';
$username = 'yourusername';
$password = 'yourpassword';
$dbname = 'poll';
$connect = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname);
?>
Look at the files.
By BlueDevil18
on Sep 28, 2008
I set everyhing up. Added a New Poll and Questions to the poll with the correct pid. However, when I browse to the file(poll.php) on the server, it still comes up “No Poll Yet” Am I not doing something?
By BlueDevil18
on Sep 28, 2008
I need some help with this. I think i set up up correctly, but i still get “no poll yet”
By Rkrueger
on Oct 12, 2008
hey i was doing this and it came up with the error that the function “max()” doesn’t have enough parameters..
can someone check this out?
By Hodde
on Oct 28, 2008
I get the “no poll yet”-message too.. and i’m also pretty sure i set the poll up correctly..
By Stonedeft
on Dec 13, 2008
You should have separated codes and html. It gets real confusing I have to strip html tags the poll.php for me to review the code.
Well I got the idea but man you should recode this to for web 2.0 standards
By VoiDeT
on Dec 13, 2008
Thank you for the comment,
Maybe you could say something like, maybe you should use objects, or logic from design separation, but web 2.0 standards haha
Web 2.0 standards in php LOL