PHP MySQL
Interactive Website Design

Create a Simple Poll

PHP and MySQL make creating interactive websites easier than ever before. Here is a simple form and processing script which you can modify and use to poll your visitors. The form on the left is a modified version.

Create the database through your PHPMySQL Admin Panel or modify the birthday scripts to create a table with the 3 fields: color, code and number

This PHP script can be used to parse forms that use checkboxes for multiple selections like the one on the left. A sample form using radio buttons is provided below.

The bolded portion of the code loops through your user's selections, if there are more than one, and adds 1 to the selected field.

<?php
$db="newdb";
$link = mysql_connect("localhost");
if (! $link) die("Couldn't connect to MySQL");
mysql_select_db($db , $link) or die("Couldn't open $db: ".mysql_error());
/*loop through the selected fields - multiple if using checkbox*/
foreach ($check as $value){
mysql_query("UPDATE simplepoll SET number=number+1 WHERE code LIKE '$value'");
}
//select the new updated values
$result = mysql_query( "SELECT color,number FROM simplepoll" ) or die("SELECT Error: ".mysql_error());
//display the updated results
$num_rows = mysql_num_rows($result);
print "<html><head><style type=text/css>
#poll{border-top :solid #004f9d 2px;border-left :solid #004f9d 2px} #poll tr td {border-bottom :solid #004f9d 2px;border-right :solid #004f9d 2px} #poll tr td {font-family: Small Fonts;font-style : normal ;font-size : 7pt; font-weight :bold } h3{font-family: Arial;font-style : normal ;font-size : 9pt; font-weight :bold;text-align : center; color :#FFFFFF;background-color :#004f9d }</style>
</head>
<body>
<table width=100%><tr valign=top><td>";
print "<table ID=poll cellspacing=0 cellpadding=5 width=125 >";
print "<tr valign=top><td colspan=2 bgcolor=#004f9d><h3>Other users chose these options.</h3></td></tr>";
while ($get_info = mysql_fetch_row($result)){
print "<tr>";
foreach ($get_info as $field )
print "\t<td>$field</td>";
print "</tr>";
}
print "</table>
</tr></table></body></html>";
mysql_close($link);
?>

The Form

The form is set in a table. We provide style sheet settings which can be placed in the head section of your document. This form uses single choice radio buttons. To change to multiple choice checkboxes simply change:
<dt><input type="radio" name="check[]" value="c1">
to
<dt><input type="checkbox" name="check[]" value="c1">

Place style sheets in head section:
#poll{ ;border-style : solid ;border-color : #004f9d ;border-width : 2px}
#poll tr td dt{font-family: Small Fonts;font-style : normal ;font-size : 7pt; font-weight :bold }


<table ID="poll" width="125" cellpadding="2" cellspacing="0" border="0">
<tr align="center" valign="top">
<td align="left" colspan="1" rowspan="1">
<h4>Select your favorite color.</h4>
<form method="POST" action="simple-poll.php">
<dl>
<dt><input type="radio" name="check[]" value="c1">Red<br>
<dt><input type="radio" name="check[]" value="c2">Green<br>
<dt><input type="radio" name="check[]" value="c3">Blue<br>
<dt><input type="radio" name="check[]" value="c4">Yellow<br>
</dl>
<input type="submit" value="Results"><input type="reset">
</form>
</td></tr></table>

Download the Scripts

Note: The poll script is not included in the download package.

The Birthdays Database management files can be downloaded in a zip file.

If using IndigoAMPP download to c:\indigoampp\apache-2.2.11\htdocs.

Extract there and you'll have a birthdays folder inside your htdocs folder. Run the scripts from there.

The package contains an integrated db management system, with a simple interface.

This Instruction file is included in the download.
Download birthdays_db.zip

MySQL Tutorial

To extend your knowledge of MySQL study the Docs and Tutorials at the official MySQL website. MYSQL.com