Script to Add Poll Vote to Database
In the code below, we show half the code needed for a website poll. The rest—in which there is a form for submitting a vote and a graph displaying the poll results—is in the file PHP-poll.php, explained in PHP Poll Script.
According to Polls & Surveys, "Easier [definition] - A survey, sometimes called a poll, is a study of what people think or believe about a topic or question. Surveys are usually done by questionnaire, interview, or observation. Harder [definition] - Public opinion polls are useful in tracing people's views on important social issues. Polls are used to assess people's preferences in political races, and the results are used to predict election results. Surveys are often employed in marketing and advertising research to measure and predict consumer's reaction to products."

We use ip tracking to validate/filter votes. Many others use ips as well, but there are others who use sessions. In the website tutorial called Build your own Survey/Poll Application, the author says "Session variables work by sending you an ID as a cookie. If you have cookies disabled then the session variable won't work, and you can vote over and over again! You might want to check that cookies are enabled in the browser." This would mean either people vote over and over and the poll is useless or you tell them to turn on cookies and they don't and the poll is useless, or you detect cookies being off and you keep them out of your poll as a result. We didn't particularly care for these options, so we went with ip address storing, the other main form of vote validation. People might have rotating (dynamic) ip addresses assigned by their ISPs, but most do not, and most of those that do will change monthly, but for the worst case scenario of hourly ip change (it could also change daily), we're still dealing with someone so incredibly motivated to multi-vote that he comes back hourly to vote—which he would have to do a LOT to significantly affect vote tallies. This is not realistic, to say the least! Even though people with the same ip (i.e., siblings) may wish to vote, the likelihood is very remote, and nothing is stopping one of them from using the home computer while another uses a work computer. Many houses have several computers, which means an avid voter can triple (or worse) his vote with ease if the cookies and sessions method is active. The ip storing method stops this. With either ip storing or cookies and sessions, voters can make use of friends' computers or work computers as a multi-vote workaround strategy. Summary: we believe the above info shows ip storing is superior to the cookies and sessions method for insuring less corrupted vote tallies.
The article just cited has a pretty nice website devoted to teaching PHP, and polls are just one of his lessons. This author relies on cookies and sessons (probably due to ease of use more than overall effectiveness). We do not. Anyway, he uses a multi-question MySQL database table where he stores questions, answers, tallies, etc. We use a single question MySQL database table where only tallies are stored—plus we store ip addresses. We compare voters' ip addresses with the stored list, and repeaters are prevented from registering a vote.
Now—on to the script code:
After using the config.php file to get the necessary magic words for db connection, we GET the answer sent from the PHP-poll.php script's radio button form. Each of the 3 radio inputs has the name "answer". The submitting of the form automatically sends the value that gets checked to the form handling script designated by the action attribute. In this script, all we need is to GET the user's vote, since all we're wishing to do in this script is to verify that the user voted, validate that he or she is indeed eligible to vote and is not a repeater, and add the answer to the correct field in the MySQL db table.
Next, if the answer from the poll form radio button values is not "choiceA" or "choiceB" or "choiceC", the form was submitted without voting so we exit the script without influencing the tallies. One could use an onsubmit event to trap nonvoters in the PHP-poll.php script, but this works just as well. Then, we use the $_SERVER['REMOTE_ADDR'] function to get the user ip address. We then search for that ip address in the ips table, and if we find it, it means he has voted already so we display the JavaScript alert "One vote only, please.", and exit. If we do not find the ip address, we enter this ip address into the ips table with the MySQL INSERT INTO command and display the alert message "Thanks for voting."
Next we SELECT everything (*) from the poll table and put the values found in the fields numA, numB, and numC into the PHP variables $numA, $numB, and $numC. We then tell the script to increment one of these 3 variables depending on whether the radio button form value selected was choiceA, choiceB, or choiceC. Finally we UPDATE the poll table and SET the 3 field values (in numA, numB, and numC) to whatever was in $numA, $numB, and $numC, remembering that only one of these three values will change—the others stay the same. Then we close the database and exit the script.
This file is named: add-poll-vote-to-database.php
<?php
include_once"config.php";
$answer=$_GET['answer'];
if($answer<>"choiceA" && $answer<>"choiceB" && $answer<>"choiceC"){$answer="";echo '<script language="javascript">alert("Please vote."); window.location="PHP-poll.php";</script>';
}else{
$poll_ip = $_SERVER['REMOTE_ADDR'];
$checkips = mysql_query("SELECT * FROM ips WHERE ip = '$poll_ip'");
if(mysql_num_rows($checkips) != 0){$answer="";echo '<script language="javascript">alert("One vote only, please.");window.location="PHP-poll.php";</script>';
}else{
$add_ip = mysql_query("INSERT INTO ips (id, ip)
VALUES('','$poll_ip')") or die(mysql_error());
echo '<script language="javascript">alert("Thanks for voting.");</script>';}
$res = mysql_query("SELECT * FROM poll") or die(mysql_error());
while ($row = mysql_fetch_array($res)){
$numA = $row['numA'];
$numB = $row['numB'];
$numC = $row['numC'];}
if($answer=="choiceA"){$numA=$numA+1;}
if($answer=="choiceB"){$numB=$numB+1;}
if($answer=="choiceC"){$numC=$numC+1;}
mysql_query("UPDATE poll SET numA = '$numA', numB = '$numB', numC = '$numC'");
mysql_close();
echo '<script language="javascript">window.location="PHP-poll.php";</script>';}
?>