system in PHP and MySQL
Before you start this lesson, please read the following
lessons for your reference:
This lesson will show you how to create a simple poll/rating
system in PHP and MySQL. The could be used for any number of items
you want to rate without changing a bit.
You may use it to do opinion poll, rate your software,images,
Here is the example:
Vote for this tutorial
Warning: mysql_connect(): Access denied for user 'chinesel_hsun'@'localhost' (using password: YES) in /home1/chinesel/public_html/kidslovepc/php-tutorial/pollresult.php on line 15
Unable to specified database
This tutorial includes 3 sections:
- Create database : how to save polling/rating results in MySQL
- Dealing poll/rate Form:
how to saving polling/rating result to database and how to set
cookie to prevent client repeat voting on the same item.
- Retrieve poll/rate result:
retrieve poll/rate result for item and assign corresponding poll/rate
icon to it.
Retrieve poll/rate result from MySQL database
Usually we need an icon to indicate a polling/rating result, such
as like this:
These icons should respond to the polling result in database. The
following code will assign an proper icon to a item according to its
polling result in database.
Assume we need put an icon besides item 1 and polling
result like result/votes(for example 3.2/46) by <?php
that this including code should be used in .php file. If you use other
HTML file, please use corresponding server side include.
Here is pollresult.php code:
//get item id
//get polling result from database
//open database connection
$username = "yourname";
$password = "yourpass"; //input your password here.
$database = "yourdb";
//connect to database
$link = mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");
$query = "select votes,result from polling where itemid=$id";
$result = mysql_query($query) or die('Error, query failed');
$votes = mysql_result($result,0,"votes");
$voteResult = mysql_result($result,0,"result");
$digit1 = substr( $voteResult,0,1); //get first digit;
$digit2 = substr( $voteResult,2,1); //the last digit;
if (strcmp($digit2,"0") == 0 )
$half = "half";
echo "<img src=\"images/$digit1$half.gif\"> $voteResult/$votes";
pollresult.php pulls voting result from database and display proper
icon based on polling results.
$voteResult = mysql_result($result,0,"result"); gives the
polling result in database.
$digit1 is the first digit of polling result.
$digti2 is the last digit of polling result. For example, if polling
result is 2.4, $digit1 is 2, $digit2 is 4.
if $digit2 is not 0, will assign an icon with "half"
in its name, such as 2half.gif
The following icons are used by this example
Here are some other polling icons: