This article will teach you how to write a basic poll script in php. You will mysql and a new version of PHP.
The first step is to create a database that will hold the information all the polls we want. For this article we will be creating a poll that will have 3 options. Yes, No, and Not Sure. So we will want to create feilds for each of those. I used Option1, Option2, and Option. We will also need a title and a question to ask. I created those as varchars. Another feild will hold the total number of votes total. The most important colum is the ID colum which will be an auto increment tinyint that will help use to select, edit, and add new records. Here is the schema of my table:
# Table structure for table `poll_data` # CREATE TABLE poll_data ( ID tinyint(4) NOT NULL auto_increment, Option1 tinyint(4) NOT NULL default '0', Option2 tinyint(4) NOT NULL default '0', Option3 tinyint(4) NOT NULL default '0', Votes tinyint(4) NOT NULL default '0', Title varchar(25) NOT NULL default '', Question varchar(50) NOT NULL default '', PRIMARY KEY (ID) ) TYPE=MyISAM; |
- Display the Title and Question of the Poll.
- Show the captions of each vote option, the percent of the total votes, and the total votes for each.
- Create a file that will allow the user to vote in the poll.
- Display the Title and Question of the Poll
a. Connect to the database
b. Select the Newest record from the database - Show Captions of each vote.
a. Select the votes for each option, and select the total votes in the poll.
b. Take the number of votes and divide by the total number of votes to find the percent. - Create a file that will allow the user to vote in the poll
a. Update the row where the user voted.
| $dbhost = "localhost"; $dbname = "YourDataBase"; $dbuser = "YourUserName"; $dbpass = "YourPass"; |
Then, we will connec to the database.
| $link_id = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname); |
Now we have some queries to do. One to select the current Title and Quesiton of the Poll, and the other to get all the information on the votes and total votes. There is a problem though. We need to get information about the current or newest poll. This is where are ID colum comes into play. We can select the Title and Question in Descending Order so that the newest record will be at the top. See, every time a new row is inserted, the ID colum has 1 added to it, or it is Auto Incremented. So, the newest row will have the largest ID, so we can sort that form Largest to Smallest to get the newest record. Also, we only want one row to be returned so we tack on Limit 1 at the end. Then we want to turn that result into an array for easy access.
| $sql = "SELECT `Title`,`Question`,`ID` FROM `poll_data` ORDER BY `ID` DESC LIMIT 1"; if(!($result = mysql_query($sql))) die(mysql_error()); $PollData = mysql_fetch_array($result) |
| $sql = "SELECT `Option1`,`Option2`,`Option3`,`Votes` FROM
`poll_data` WHERE `ID` = '" . $PollData['ID'] . "' ORDER BY `ID` DESC
LIMIT 1"; if(!($result = mysql_query($sql))) die(mysql_error()); if(!($VoteData = mysql_fetch_array($result))) die(mysql_error()); |
|
if($VoteData["Option1"] != 0) { $VotePercent1 = Round(($VoteData["Option1"] / $VoteData["Votes"]) * 100) . "%"; } else { $VotePercent1 = 0 ."%"; } if($VoteData["Option2"] != 0) { $VotePercent2 = Round(($VoteData["Option2"] / $VoteData["Votes"]) * 100) . "%"; } else { $VotePercent2 = 0 ."%"; } if($VoteData["Option3"] != 0) { $VotePercent3 = Round(($VoteData["Option3"] / $VoteData["Votes"]) * 100) . "%"; } else { $VotePercent3 = 0 ."%"; } |
|
<html> <head>> <title>Basic Poll - Written by Adman</title> </head> <body> <form method="POST" action="vote.php"> <table width="500" border="1" cellspacing="0" cellpadding="8"> <tr> <td colspan="3"><b><?=$PollData['Title']?> - <?=$PollData['Question']?></b></td> </tr> <tr> <td width="35%"> <input type="radio" name="Vote" value="Option1"> Yes</td> <td width=60%> <img src="/img_articles/4349/bar.gif" width="<?=$VotePercent1?>" height="20"> </td> <td><?=$VoteData["Option1"]?> Votes</td> </tr> <tr> <td width="35%"> <input type="radio" name="Vote" value="Option2"> No </td> <td width=60%> <img src="/img_articles/4349/bar.gif" width="<?=$VotePercent2?>" height="20"> </td> <td><?=$VoteData["Option2"]?> Votes</td> </tr> <tr> <td width="35%"> <input type="radio" name="Vote" value="Option3" > Not Sure</td> <td width="60%"> <img src="/img_articles/4349/bar.gif" width="<?=$VotePercent3?>" height="20"> </td> <td><?=$VoteData["Option3"]?> Votes</td> </tr> <tr> <td colspan="3"> <center> <input type="submit" name="Submit" value="Vote"> </center> </td> </tr> </table> </form> </body> </html> |
if(empty($_POST["Vote"]))
die("You did not enter your vote");
|
| $dbhost = "localhost"; $dbname = "misc"; $dbuser = "root"; $dbpass = "trigger"; $link_id = mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname); $sql = "SELECT `Option1`,`Option2`,`Option3`,`Votes`,`ID` FROM `poll_data` ORDER BY `ID` DESC LIMIT 1"; if(!($result = mysql_query($sql))) die(mysql_error()); if(!($PollData = mysql_fetch_array($result))) die(mysql_error()); |
| if($_POST["Vote"] == "Option1") { $Votes1 = $PollData["Option1"] + 1; $TotalVotes = $PollData["Votes"]+ 1; $sql = "UPDATE `poll_data` SET `Option1`='$Votes1', `Votes`='$TotalVotes' WHERE `ID` = '". $PollData['ID'] . "' LIMIT 1"; if(!($result = mysql_query($sql))) die(mysql_error()); echo "Vote successful! <a href=\"index.php\">Back</a> to the poll."; } else if ($_POST["Vote"] == "Option2"){ $Votes2 = $PollData["Option2"] + 1; $TotalVotes = $PollData["Votes"]+ 1; $sql = "UPDATE `poll_data` SET `Option2`='$Votes2', `Votes`='$TotalVotes' WHERE `ID` = '". $PollData['ID'] . "' LIMIT 1"; if(!($result = mysql_query($sql))) die(mysql_error()); echo "Vote successful! <a href=\"index.php\">Back</a> to the poll."; } else { $Votes3 = $PollData["Option3"] + 1; $TotalVotes = $PollData["Votes"] + 1; $sql = $sql = "UPDATE `poll_data` SET `Option3`='$Votes3', `Votes`= '$TotalVotes' WHERE `ID` = '". $PollData['ID'] . "' LIMIT 1"; if(!($result = mysql_query($sql))) die(mysql_error()); echo "Vote successful! <a href=\"index.php\">Back</a> to the poll."; } |





