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 /home/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 /home/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/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";
}
?> |
|