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. 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_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:

  1. Create database : how to save polling/rating results in MySQL
  2. 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.
  3. Retrieve poll/rate result: retrieve poll/rate result for item and assign corresponding poll/rate icon to it.

 

Poll/Rating Form

The code for Poll/Rating form (shown in above example) is the following:

<form method="post" action="vote.php">
<select name="select" size="1">
<option value="-1">Please select one</option>
<option value="5">Excellent</option>
<option value="4">Very good</option>
<option value="3">Good</option>
<option value="2">Fair</option>
<option value="1">Bad</option>
</select>
<input type="submit" name="vote" value="Vote">
<input name="itemid" type="hidden" id="itemid" value="1">
</form>

The form could be put besides any item you want to rate. Just change that hidden field "itemid" value to its proper id and make sure that id exists in database.

Dealing with vote result

vote.php is dealing with result.

<?php

if(isset($_POST['vote']))
{
//get voting value;
$votevalue=$_POST['select'];

//a simple validation
if($votevalue == -1)
{
echo "Please go back and select one rating option";
exit;
}

//===============================
//saving vote data to database
//===============================

//open database connection
$username="yourname";

$password="yourpassword"; //input your password here.
$database="yourdatabase";
//connect to database
$link=mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");

//retrieve old voting result
$id=$_POST['itemid'];
$query="select votes,result from polling where itemid=$id";
$result=mysql_query($query) or die('Error, query failed');

$oldVote=mysql_result($result,0,"votes");
$oldResult=mysql_result($result,0,"result");

//calculate new result
$newVote=$oldVote+1;
$newResult=($oldVote*$oldResult+$votevalue)/$newVote;

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

echo "Thanks for your voting";

}
?>

vote.php did 3 things:

  • validate input
  • retrieve old polling/voting result
  • saving new polling/voting result

First, vote.php did a simple validation. If user didn't make selection, it will prompt "Please go back and select one rating option" and exit program.

In order to get correct voting result, vote.php pulls out the old voting result by using query "select votes,result from polling where itemid=$id"

The new result is calculated by the formula:

$newResult=($oldVote*$oldResult+$votevalue)/$newVote;

The last step is to save the new voting result back to database. "update polling set votes=$newVote,result=$newResult where itemid=$id" did this chore for you.

Set cookie

You probably don't want a single user to vote on one item again and again and again. It might waste your bandwidth and trash your voting schism.

In order to prevent someone trashing your system, you may set cookie to control how often a user could vote on the same item.

The lesson "Set cookie and delete cookie" has more detail about how to set cookie. We just brief it here:

setcookie("vote1","v",time()+30*24*60*60); //expire after 30 days.

$_COOKIE("vote1");

Suppose cookie name is the format: vote+item id. It means cookie name for item 2 is vote2 and so on.

So, in vote.php, we may first check if the user had voted on this item or not by checking on his cookie. If there is a cookie, discard his voting. Otherwise, save voting result to database and set a cookie.

isset($_COOKIE['vote$id']) checks on cookie setting.

setcookie("vote$id","v",time()+30*24*60*60); sets the cookie expired in 30 days.

After adding cookie validation vote.php could look like this:

<?php

if(isset($_POST['vote']))
{
//get voting value;
$votevalue=$_POST['select'];
$id=$_POST['itemid'];

//a simple validation
if($votevalue== -1)
{
echo "Please go back and select one rating option";
exit;
}

//cookie validation
if(isset($_COOKIE["vote$id"]))
{
echo "Thanks for voting. But... you already vote on this.";
exit;
}


//set cookie expired in 30 days.
setcookie("vote$id","v",time()+30*24*60*60);

//===============================
//saving vote data to database
//===============================

//open database connection
$username="yourname";
$password="yourpassword"; //input your password here.
$database="yourdatabase";
//connect to database
$link=mysql_connect(localhost,$username,$password);
@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');

$oldVote=mysql_result($result,0,"votes");
$oldResult=mysql_result($result,0,"result");

//calculate new result
$newVote=$oldVote+1;
$newResult=($oldVote*$oldResult+$votevalue)/$newVote;

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

echo "Thanks for your voting";

}
?>

Back: create poll database Next: retrieve poll result
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
Variables
Constants
Operators(part1)
Operators(part2)
Logic Structures
Include files
Loops
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 © kidslovepc.com All rights reserved(new). Privacy|Copyright
designed by info@kidslovepc.com

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