Hudzilla.org - the homepage of Paul Hudson
Contents > Practical PHP > Creating a poll Wish List | Report Bug | About Me ]

22.1.2     Development: creating the simplest poll

This is NOT the latest copy of this book; click here for the latest version.

Starting with the SQL, we can create the table containing poll questions simply by fleshing out the schema already shown. As per usual we will need an ID primary key so that we can work with rows easily, but we should also set the default value of the vote counters to 0.

Here is how that looks in SQL:

CREATE TABLE pollquestions (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Question CHAR(255), Answer1 CHAR(255), Answer2 CHAR(255), Answer3 CHAR(255), Answer1_Votes INT DEFAULT 0, Answer2_Votes INT DEFAULT 0, Answer3_Votes INT DEFAULT 0);

To get us started off, here's some SQL to add the first poll question:

INSERT INTO pollquestions (Question, Answer1, Answer2, Answer3) VALUES ('Favourite film?', 'Casablanca', 'Aliens', 'Indiana Jones');

The first page needs to read the latest poll question and its answers, and present it all as an HTML form. Save this next script as poll.php:

<?php
    mysql_connect
("localhost", "phpuser", "alm65z");
    
mysql_select_db("phpdb");
    
    
$result = mysql_query("SELECT ID, Question, Answer1, Answer2, Answer3 FROM pollquestions ORDER BY ID DESC LIMIT 1;");
    
extract(mysql_fetch_assoc($result), EXTR_PREFIX_ALL, 'poll');
    
    echo
"<FORM METHOD=\"POST\" ACTION=\"results.php\">";
    echo
"<B>$poll_Question</B><BR />";
    echo
"<INPUT TYPE=\"RADIO\" NAME=\"vote\" VALUE=\"1\">$poll_Answer1</OPTION><BR />";
    echo
"<INPUT TYPE=\"RADIO\" NAME=\"vote\" VALUE=\"2\">$poll_Answer2</OPTION><BR />";
    echo
"<INPUT TYPE=\"RADIO\" NAME=\"vote\" VALUE=\"3\">$poll_Answer3</OPTION><BR />";
    echo
"<INPUT TYPE=\"SUBMIT\" VALUE=\"Vote\" />";
    echo
"<INPUT TYPE=\"HIDDEN\" NAME=\"poll\" VALUE=\"$poll_ID\" />";
    echo
"</FORM>";
?>

Note that the SQL query uses "ORDER BY ID DESC LIMIT 1" to pick out the most recently added poll, and that the ACTION attribute of the form is set to point to results.php, which is the second file.

Here is the code for results.php:

<?php
    mysql_connect
("localhost", "phpuser", "alm65z");
    
mysql_select_db("phpdb");
    
    if (isset(
$_POST['vote'])) {
        
$votenum = $_POST['vote'];
    
        switch(
$votenum) {
            case
1: $votechange = "Answer1_Votes"; break;
            case
2: $votechange = "Answer2_Votes"; break;
            case
3: $votechange = "Answer3_Votes"; break;
            default: exit;
        }
    
        
$result = mysql_query("UPDATE pollquestions SET $votechange = $votechange + 1 WHERE ID =
        {$_POST['poll']};"
);
        if (
$result) {
            echo
"Thanks for voting!<BR /><BR />";
        }
    }
    
    echo
"<B>Results of poll:</B><BR />";
    
    
$result = mysql_query("SELECT Answer1, Answer2, Answer3, Answer1_Votes, Answer2_Votes, Answer3_Votes
    FROM pollquestions ORDER BY ID DESC LIMIT 1;"
);
    
extract(mysql_fetch_assoc($result), EXTR_PREFIX_ALL, 'poll');
    
    echo
"<UL>";
    echo
"<LI>$poll_Answer1: $poll_Answer1_Votes</LI>";
    echo
"<LI>$poll_Answer2: $poll_Answer2_Votes</LI>";
    echo
"<LI>$poll_Answer3: $poll_Answer3_Votes</LI>";
    echo
"</UL>";
?>

That is quite a long script, but much of it should be fairly straightforward. The switch/case statement block is there to turn the vote numbers into the name of the relevant field in the table. This could have been done by setting the field name as the answer number in poll.php, but that would basically have allowed malicious users to pass any variable into our script for change in the table - not a smart move. The code above validates that a choice of either 1, 2, or 3 has been passed in, as anything else will make the script exit.

Those scripts work fine - go ahead and test them out.





<< 22.1.1 Analysis: what makes a web poll?   22.1.3 Analysis: Poll v2 >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
starhamayoun@gmail.com - 07 Sep 2008

it is fine but anyone can save the file ang get my username and passwerd from open with i-e notpad

A PHP User - 07 Sep 2008

The code works fine. Howerver, why don't you add a "admin page" to add new questions. And the layout for results.php could be better.

A PHP User - 07 Sep 2008

Whats the o/p of above codes . and how to run this code



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is six plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow