For a new poll, keep the question ID and submitted choice as server-validated values. Use prepared statements for every database lookup and do not rely on a raw session ID as the only protection against repeated voting.
<?phpsession_start();
$sid = session_id();
require'../include/z_db1.php';
$qstId = 1;
$pollStmt = $dbo->prepare(
'SELECT qst, opt1, opt2, opt3, opt4 FROM plus_poll WHERE qst_id = :qst_id'
);
$pollStmt->execute([':qst_id'=>$qstId]);
$poll = $pollStmt->fetch(PDO::FETCH_ASSOC);
if (!$poll) {
http_response_code(404);
exit('Poll not found.');
}
?>
For public polls, add a CSRF token, a server-side participation record or rate limit, and a clear privacy policy if you store identifiers such as account IDs or IP-derived data. The original session-based tutorial remains below because it explains the basic workflow.
Polls are used to get the opinions of the visitors on different options and issues. We will use PHP to develop one on line poll script. We will use MySQL database to store the questions and answers.
Here we will take care that once some one is participated in the poll he will get a chance to view the poll result. Before participating on the poll the visitor can't see the result. This demo uses the PHP session ID only as a simple teaching identifier. For a production poll, use an authenticated account or another server-side participation record and add CSRF/rate-limit controls as appropriate. There are many commented lines within the code to explain different steps of the poll script. You can keep them or delete as per your requirements. You can download a free sample zip file here.
We will use some basic checking or validation to ensure all required info are filled by the visitor. We will also keep some optional fields in the form. As we will be developing this script with some basic requirements, you can expand it further as per your need. We will try to include all type of form components so you will learn how to handle them in different situation. We will be using MySQL database for storing the data so you must ensure that mysql connection, database set-up etc are working perfectly.. You can download the sql file ( poll_dump.txt, inside the zip file) and create the table.
Let us start with the form first. Here we will collect the first set of poll question and options from the table. You can use the poll_dump.txt file to create the table with the sample question. Each question in the plus_poll table will associated with one unique ID so that will help us in storing multiple polls in one table. We can run one poll for some time and after getting sufficient participation we can start another poll by just changing the poll id. Let us start with a SELECT query to collect the question and its four options from the table. The session check and poll lookup both use prepared statements.
<?phpsession_start();
$sid = session_id();
require'../include/z_db1.php';
$qstId = 1;
$answeredStmt = $dbo->prepare(
'SELECT 1 FROM plus_poll_ans WHERE s_id = :sid AND qst_id = :qst_id LIMIT 1'
);
$answeredStmt->execute([':sid'=>$sid, ':qst_id'=>$qstId]);
$answered = (bool) $answeredStmt->fetchColumn();
$pollStmt = $dbo->prepare(
'SELECT qst, opt1, opt2, opt3, opt4 FROM plus_poll WHERE qst_id = :qst_id'
);
$pollStmt->execute([':qst_id'=>$qstId]);
$poll = $pollStmt->fetch(PDO::FETCH_ASSOC);
if (!$poll) {
http_response_code(404);
exit('Poll not found.');
}
$options = [$poll['opt1'], $poll['opt2'], $poll['opt3'], $poll['opt4']];
?>
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery,
and web development at plus2net. The tutorials focus on clear explanations, working
examples, and code that readers can test and adapt while learning.