PHP Online opinion poll script using MySQL

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. To do this we will store PHP session id to identify who has already participated on the poll. 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 the select query to collect the question and its four options from the table.
session_start();
$s_id=session_id();
require "config.php";
//////////////////////////////
/* Read the session id to 
display the poll result link 
*/

$count=$dbo->prepare("select s_id from plus_poll_ans where s_id='$s_id'");
$count->execute();
$no=$count->rowCount();
//echo $no;
if($no <=0 ){
echo "<a href=view_poll_result.php>View the poll result</a>";
}

/* You can keep the parts below inside the else area if you don't want to 
show the form for the visitors who have already participated in the poll
*/
$qst_id=1; // Change this ID for a different poll
$count=$dbo->prepare("select * from plus_poll where qst_id=:qst_id");
$count->bindParam(":qst_id",$qst_id,PDO::PARAM_INT,1);
if($count->execute()){
//echo " Success <br>";
$query = $count->fetch(PDO::FETCH_OBJ);
}

//$query=mysql_fetch_object(mysql_query("select * from plus_poll where qst_id=$qst_id"));
echo "
<form method=post action=poll_displayck.php>
<input type=hidden name=qst_id value=$qst_id>
<table border='1' cellspacing='0'  bordercolor='#ffff00' width='320' id='AutoNumber1'>
  <tr>
  <td width='100%' bgcolor='#ffff00'><font face='Verdana' size='2' >$query->qst</font></td>
  </tr>
  <tr>
  <td width='100%' >
  <table border='0' cellspacing='0'  bordercolor='#111111' width='100%'  cellpadding='0'>
      <tr bgcolor='#f1f1f1'>
  <td width='10%'><input type='radio' value='$query->opt1'  name='opt'></td>
  <td width='90%'><font face='Verdana' size='2' >$query->opt1</font></td>
  </tr>
  <tr>
  <td width='10%'><input type='radio' value='$query->opt2'  name='opt'></td>
  <td width='90%'><font face='Verdana' size='2' >$query->opt2</font></td>
  </tr>
  <tr bgcolor='#f1f1f1'>
  <td width='10%'><input type='radio' value='$query->opt3'  name='opt'></td>
  <td width='90%'><font face='Verdana' size='2' >$query->opt3</font></td>
  </tr>
  <tr>
  <td width='10%'><input type='radio' value='$query->opt4'  name='opt'></td>
  <td width='90%'><font face='Verdana' size='2' >$query->opt4</font></td>
      </tr>
    </table>
    </td>
  </tr>
<tr>
    <td width='100%' bgcolor='#ffff00' align=center>
<font face='Verdana' size='2' ><input type=submit value=Submit></form></td>
  </tr>

</table>
";
/* End of the form displaying the poll question and its four options for selection */



Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com




    Post your comments , suggestion , error , requirements etc here .




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer