How to set up a voting booth tutorial in PHP and MySQL

 

NAMI-MASS, one of my long-term clients, has the humblest of UNIX servers, so I wrote this Voting for the Best Banner page that tabulated votes and voters in a little under two hours. It's easy to do.

First of all, the voting page, carefully set up with radio buttons for each picture to be voted on and the radio button names, go to this page and View Source for results, this page can be a html page as well as a PHP...

 

Now for the results page, in PHP of course, what do you want it to do, count the votes including the one that the voter is submitting? Also, send an email, if the page is submitted, but don't add anything to the database unless last name and email are submitted. And sending the LastName, FirstName, and Email to a MySQL database. Also has error msgs if Last Name and Email aren't filled out.

Here's the code for that...

 

How to do the resulting page from the voting page

Submit bannervote.php page

How to show MySQL database results
http://juliagreen.com/juliaphp/banner/bannerall.php

How to show vote count from MySQL database
http://juliagreen.com/juliaphp/banner/bannercount.php

 

How to do the resulting page from the voting page - bannervote3.php

<?php $con=mysql_connect("localhost", "juliaphp", "password") or die("Connect Error: ".mysql_error());

$db="juliaphp";

mysql_select_db($db, $con)or die("Connect Error: ".mysql_error());

$email2=$_POST['email2'];
$lastname=$_POST['lastname'];
$firstname=$_POST['firstname'];
$bannervote=$_POST['BannerVote'];
$youremail="julia@juliacomputers.com";
$email3="julia@juliacomputers.com";
$yourname="Julia Green";
$yourphone="";
$yourorg="Julia Computer Consulting";
$yoururl="http://www.juliagreen.com";
$yourfax="";
$bgcolor="silver";
$bgpic="";
$fontcolor="navy";
$fontsize="4";
$redirect="";
$output1line="Thank you for submitting your Banner Vote!";
$output2line="You will be receiving a confirming email shortly.";
$endingline1="Please email us with any further questions...";
$endingline2="Thank you again!";
$beginemail="Thank you for submitting your email!";
$endemail="For more information, please contact:";
$subjecttoyou="Response Mailing for Banner Vote";
$subjecttoclient="Thank you for submitting your After The Walk Banner Vote!";

?>

 

<?php

$senderemail = $youremail;
$sendername = $_POST['lastname'];
$recipient = "$email2, $email3";
$subject = "$subjecttoclient";
$mailheader = "From: $senderemail\n";
$mailheader .= "Reply-To: $senderemail\n\n";
$message = "Thank you for voting in our Banner Vote Contest and thank you for reading the NAMI-MASS E-NEWS! The winner will be announced September 26th! Good luck!\n\n";
$message .="The results of your form are as follows:\n\n";
$message .= "Full Name: $lastname $firstname\n";
$message .= "Your banner vote was for: $bannervote\n\n";

mail($recipient, $subject, $message, $mailheader) or die ("Failure");

?>

<?php

if (($lastname=="") OR ($email2=="")){
echo("<table width=50% bgcolor=silver align=center><th><font size=+1>You are missing the following variables. Backspace to correct: </th><br>");
}
if ($lastname==""){
echo ("<tr><td><font size=+1>Last Name<br></td></tr>");
}
if ($email2==""){
echo ("<tr><td><font size=+1>Valid Email Address</td></tr></table>");
}

elseif (($lastname!=="") AND ($email2!=="")) {

echo("<body bgcolor=$bgcolor text=$fontcolor>");

 

echo("<font size=$fontsize>$output1line<br>Your form was sent $now from <a href=mailto:$email2>$email2</a>.<br><br><hr>\n\n");

echo("<font size=$fontsize><br>Thank you for voting in our Banner Vote Contest and thank you for reading the NAMI-MASS E-NEWS! Good luck!\n\n<hr>");

echo("<font size=$fontsize><br>For more information, please contact:<br><br>$yourname<br>$yourorg<br><a href=mailto:$youremail>$youremail</a><br><a href=$yoururl>$yoururl</a><br>Telephone: $yourphone<br><br>");

echo("<font size=$fontsize><br>$ouput2line");
}

?>

 

<?php

if (($lastname!=="") AND ($email2!=="")) {
$sql = "insert into bannervote (email2, lastname, firstname, bannervote) values ('$email2', '$lastname', '$firstname', '$bannervote')";

 

 

$result = mysql_query("SELECT * FROM bannervote WHERE bannervote='$bannervote'");
$num_rows = mysql_num_rows($result);
$num_rows2=$num_rows+1;
echo "$bannervote -- This banner has ";
echo "$num_rows2 Votes\n";

mysql_query($sql)or die("Your error statement here".mysql_error());

}
mysql_close($con);
?>

How to Show MySQL Results from the bannervote Table

This is just one page. bannerall.php

<HTML>
<HEAD>
<title>Banner Count List of Names in Database</title>
</HEAD>
<BODY>
<FONT COLOR="663399">

<h1>Banner Vote Database</h1>

<table width="90%" border="0" cellspacing="0" cellpadding="0">

<BR>

 

<?php $con=mysql_connect("localhost", "juliaphp", "password") or die("Connect Error: ".mysql_error());

$db="juliaphp";

mysql_select_db($db, $con)or die("Connect Error: ".mysql_error());

$sql="select * from bannervote ORDER BY timestamp ASC";

$result= mysql_query("$sql")or die("Connect Error: ".mysql_error());

 

while ($row = mysql_fetch_object($result)) { ?>
Timestamp: <?php echo $row->timestamp; ?></b><br />

<FONT SIZE="2"><?php if ($row->firstname !== ""){ ?><?php echo $row->firstname; }?> <?php if ($row->lastname !== ""){echo $row->lastname; ?></i><br /><?php }?>
<b>Email Address: <?php echo $row->email2; ?></b><br />
Voted for Banner: <?php echo $row->bannervote; ?><br />

<br /><br /></font></table>

<?php
}

mysql_free_result($result);

mysql_close($con);
?>

</BODY>
</HTML>

 

How to show vote count from MySQL database

<HTML>
<HEAD>
<title>Banner List of Votes - 2005</title>
</HEAD>
<BODY>
<FONT SIZE="+2" COLOR="663399">

<h1>List of Banner Items</h1>

<table width="90%" border="0" cellspacing="0" cellpadding="0">

<BR>

 

<?php $con=mysql_connect("localhost", "juliaphp", "password")or die("Connect Error: ".mysql_error());

$db="juliaphp";

mysql_select_db($db, $con)or die("Connect Error: ".mysql_error());

$result = mysql_query("SELECT * FROM bannervote WHERE bannervote='Baybridge'");
$num_rows = mysql_num_rows($result);
echo "Baybridge banner has ";
echo "$num_rows Votes\n\n";
echo "<hr>";

$result = mysql_query("SELECT * FROM bannervote WHERE bannervote='CentralMiddlesex'");
$num_rows = mysql_num_rows($result);
echo "Central Middlesex banner has ";
echo "$num_rows Votes\n\n";
echo "<hr>";

$result = mysql_query("SELECT * FROM bannervote WHERE bannervote='Central'");
$num_rows = mysql_num_rows($result);
echo "Central banner has ";
echo "$num_rows Votes\n\n";
echo "<hr>";

$result = mysql_query("SELECT * FROM bannervote WHERE bannervote='Crossroads'");
$num_rows = mysql_num_rows($result);
echo "Crossroads banner has ";
echo "$num_rows Votes\n\n";
echo "<hr>";

$result = mysql_query("SELECT * FROM bannervote WHERE bannervote='Elmbrook'");
$num_rows = mysql_num_rows($result);
echo "Elmbrook banner has ";
echo "$num_rows Votes\n\n";
echo "<hr>";

$result = mysql_query("SELECT * FROM bannervote WHERE bannervote='GreaterLawrence'");
$num_rows = mysql_num_rows($result);
echo "Greater Lawrence banner has ";
echo "$num_rows Votes\n\n";
echo "<hr>";

$result = mysql_query("SELECT * FROM bannervote WHERE bannervote='MACC'");
$num_rows = mysql_num_rows($result);
echo "MACC banner has ";
echo "$num_rows Votes\n\n";
echo "<hr>";

$result = mysql_query("SELECT * FROM bannervote WHERE bannervote='NAMINW'");
$num_rows = mysql_num_rows($result);
echo "NAMINW banner has ";
echo "$num_rows Votes\n\n";
echo "<hr>";

$result = mysql_query("SELECT * FROM bannervote WHERE bannervote='PeaseFamily'");
$num_rows = mysql_num_rows($result);
echo "Pease Family banner has ";
echo "$num_rows Votes\n\n";
echo "<hr>";

$result = mysql_query("SELECT * FROM bannervote WHERE bannervote='PIN'");
$num_rows = mysql_num_rows($result);
echo "PIN banner has ";
echo "$num_rows Votes\n\n";
echo "<hr>";

$result = mysql_query("SELECT * FROM bannervote WHERE bannervote='PointAfter'");
$num_rows = mysql_num_rows($result);
echo "Point After banner has ";
echo "$num_rows Votes\n\n";
echo "<hr>";

$result = mysql_query("SELECT * FROM bannervote WHERE bannervote='SSMH'");
$num_rows = mysql_num_rows($result);
echo "SSMH banner has ";
echo "$num_rows Votes\n\n";
echo "<hr>";

$result = mysql_query("SELECT * FROM bannervote WHERE bannervote='Worthington'");
$num_rows = mysql_num_rows($result);
echo "Worthington banner has ";
echo "$num_rows Votes\n\n";
echo "<hr>";

mysql_close($con);
?>

</BODY>
</HTML>