A Poll/Rating
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,
news, music,anything.
Here is the example:
Vote for this tutorial
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 2 in /home6/chinesel/public_html/kidslovepc/php-tutorial/pollresult.php on line 22
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 2 in /home6/chinesel/public_html/kidslovepc/php-tutorial/pollresult.php on line 23
/
|
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.
Poll database in MySQL
We need a database to save polling/rating result, such as item id
(which software is rated), how many times rated and the average rating/polling
result.
The following is the SQL query for creating a polling table in MySQL.
CREATE TABLE `polling` (
`itemid` INT NOT NULL ,
`votes` INT NOT NULL ,
`result` DECIMAL(
2, 1 ) NOT NULL ,
PRIMARY KEY ( `itemid` )
) TYPE = MYISAM ;
Using phpMyAdmin
Create Database could show you how to use PhpMyAdmin to create
above table. Note that for result column, which has
type DECIMAL(2,1), you need put 2,1 in "Length/Values*"
field, shown in the following image:
Decimal(2,1) means result column has 2 digits in
total, one digit after digit point. Since we only want rate our items
like: no rating(0),bad(1),fair(2), good(3),very good(4) and excellent(5),
the average result will be between 0.0 and 5.0,like 3.2, 4.5, etc.
If you want more precise result, you may use more digits in decimal
type, for example Decimal(7,4).
In this lesson, we make rating on one item for demo purpose. Before
we do the polling/rating, the rated item should exist in database.
You may use PhpMYAdmin input the following record to your polling
table:
itemid : 1, votes: 0, result: 0.
In PHHMYAdmin panel, click on Insert tab and input data like the
following image:
|