KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones

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 using radio button. The could be used for any number of items you want to rate without changing a bit.

This polling/rating example uses radio buttons. It uses the same pollresult.php as in lessonPolling/Rating System in PHP. The vote.php is changed slightly since we don't want to prompt user.

This example won't explain code very detail like the lesson Polling/Rating System in PHP.

You may use it to do opinion poll, rate your software,images, news, music,anything.

Here is the modified example:

Vote for this tutorial:
Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home1/chinesel/public_html/kidslovepc/php-tutorial/php-poll-rating-radio.php on line 75

Warning: include( failed to open stream: no suitable wrapper could be found in /home1/chinesel/public_html/kidslovepc/php-tutorial/php-poll-rating-radio.php on line 75

Warning: include(): Failed opening '' for inclusion (include_path='.:/usr/php/56/usr/share/pear') in /home1/chinesel/public_html/kidslovepc/php-tutorial/php-poll-rating-radio.php on line 75

Poor Good

Using session variable to control voting frequency

Suppose you want to rate an tutorial,which itemid is 24. You need an form like the above one:

<?php session_start()?>
<?php virtual("vote.php") ?>
Vote for this tutorial: <strong>
<?php include ''; ?>
<form action="" method="post">
<input type="radio" name="select" value="1">
<input type="radio" name="select" value="2">
<input type="radio" name="select" value="3">
<input type="radio" name="select" value="4">
<input name="select" type="radio" value="5" checked>
<input type="submit" name="vote" value="Vote">
<input name="itemid" type="hidden" id="itemid" value="24">

Please note that:

  • <?php session_start()?>
    must be put in the first line, no empty space before it.
  • <?php include ''; ?> is pulling out voting result.

For multiple items in one page, you only need include vote.php for once on the top. But you need call <?php include ''; ?> for different itemid XXX.

We use session variable to control voting frequency since we don't like one user vote it again and again.

We set session variable for each voting item. Then we check it in PHP code. If the session variable set, ignore the vote. Otherwise save it to database. For simplicity, the session variable for item 24 could be "vote24", for item 2 is "vote2" and so on.

Here is the code of vote.php for this example.


//get voting value;


//check session
if( !isset($_SESSION["vote$id"])) //if session variable is not set

//saving vote data to database

//open database connection
$username = "yourname";
$password = "yourpassword"; //input your password here.
$database = "yourdb";
//connect to database
@mysql_select_db($database) or die("<b>Unable to specified database</b>");

//retrieve old voting result

$query="select votes,result from polling where itemid=$id";
$result=mysql_query($query) or die('Error, query failed');


//calculate new result

//saving new result to database
$query="update polling set votes=$newVote,result=$newResult where itemid=$id";
mysql_query($query) or die('Error, query failed');

} //end of checking session


Reference: Polling/Rating System in PHP
PHP Calendar
Comprehensive PHP Web Calendar
Configurable PHP Calendar
PHP Calendar
PHP Image Processing
PHP grahic softwares
PHP create real-time dynamic chart
create antispam image(image verification)
Add water mark to image
PHP Image processing introduction
Add text on Image
Create thumbnail for image
Create image from text on fly
PHP code sample
PHP create dynamic PDF with FPDF
PHP create dynamic PDF with PDFLib
PHP create dynamic PDF with ClibPDF
Improved PHP configurable hit counter
Install phpMyAdmin,MySql, PHP
PhpMyAdmin export/import data
A simple PHP RSS feed reader
PHP include (SSI)
Working with Forms
PHP formmail Generator
Using phpMyAdmin Create Database
Insert Form Data into Database
Retrieve data from Database
Retrieve multiple records
Batch insert text using phpMyAdmin
Using phpMyAdmin insert binary file
Insert binary file using PHP code
Using form insert binary file into MySQL
Retrieve binary file from MySQL
Image hosting business in minutes
Send HTML mail in PHP
Send bulk emails in PHP
Polling/Rating System in PHP
Polling/Rating System in PHP(radio button)
Check visitor's IP address
Check reciprocal link
PHP tutorial
What is PHP?
Install PHP on Windows XP (part1)
Install PHP on Windows XP (part2)
PHP open and closing tags
PHP types
Logic Structures
Include files
Set cookie and delete cookie
Most often used MySQL functions



Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © All rights reserved(new). Privacy|Copyright
designed by

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer