I need to troubleshoot PHP code to store the results of a poll, and to display the results. I have started the code but am really confused how to properly use the CREATE DATABASE, CREATE TABLE, INSERT and SELECT statements.
I need help with this, as well as how to display the results in a similar fashion to the below:

Your favorite flower is the Rose.

There are 500 total votes.

Rose received 10% of the total vote.
Daisy received 20% of the total vote.
Other received 70% of the total vote.

Please assist me with structuring the code, and/or direct me to a similar poll on the Internet, so that I may actually see code that works. Examples of how to use the above statements would be very helpful.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>My Favorites</title>
</head>
<body>
<FORM style="MARGIN-TOP: 0px; MARGIN-LEFT: 0px; MARGIN-RIGHT: 0px" 
name=Favorites action=Pollresults2.php method=post 
target="">
<table width="33%" border="1" align="center" bordercolor="#333399">
<tr bordercolor="#333399">
<th scope="col">
<TABLE width=133 border=1 cellPadding=2 cellSpacing=0 bordercolor="#333399" bgColor=#ddeeff>
<TBODY>
<TR>
<TD bgColor=#5588cc>
<TABLE width="100%" border=1 cellPadding=0 cellSpacing=0 bordercolor="#FFFFFF">
<TBODY>
<TR>
<TD align=middle>
<div align="center"><FONT face=verdana,arial,sans-serif color=#ffffff size=2><B>My Favorites</B></FONT></div>
</TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
<TR>
<TD>
<div align="center"><FONT face=verdana,arial,sans-serif color=#000000 size=1>Which is your favorite Flower?<BR>
</FONT>
<TABLE border=1 bordercolor="#333399">
<TBODY>
<TR>
<TD vAlign=top>
<INPUT type=radio value=Daisy name=Flower>
</TD>
<TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000 
size=1>Daisy</FONT></TD>
</TR>
<TR>
<TD vAlign=top>
<INPUT type=radio value=Rose name=Flower>
</TD>
<TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000 
size=1>Rose</FONT></TD>
</TR>
<TR>
<TD vAlign=top>
<INPUT type=radio value=Tulip name=Flower>
</TD>
<TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000 
size=1>Tulip</FONT></TD>
</TR>
<TR>
<TD vAlign=top>
<input type=radio value=Lily name=Flower>
</TD>
<TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000 
size=1>Lily</FONT></TD>
</TR>
<TR>
<TD vAlign=top>
<INPUT type=radio value=Orchid name=Flower>
</TD>
<TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000 
size=1>Orchid</FONT></TD>
</TR>
<TR>
<TD vAlign=top>
<INPUT type=radio value=Lilac name=Flower>
</TD>
<TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000 
size=1>Lilac</FONT></TD>
</TR>
<TR>
<TD vAlign=top>
<INPUT type=radio value=Other name=Flower>
</TD>
<TD vAlign=center><FONT face=verdana,arial,sans-serif color=#000000 
size=1>Other</FONT></TD>
</TR>
</TBODY>
</TABLE>
<BR>
</div>
</TD>
</TR>
</TBODY>
</TABLE>
</th>
<br>
<br>
<table width="100%"  cellspacing="5" cellpadding="5">
<tr>
<th scope="col">
<INPUT type=submit value="Submit Vote" name=poll_submit>
</th>
</tr>
</table>
<br>
</FORM>
</body>
</html>
</head> 
<body> 
<form action="Poll2.html" method="get"> 
<style type="text/css"> 
<!-- 
.style1 { 
color: #333399; 
font: bold 24px Arial; 
text-align: center; 
 
--> 
</style> 
<p class="style1">Le Fleur!</p> 
<strong><p class="style5"> Thank you for your vote. Below are the results. 
<br> 
If you did not vote, please hit the back button and do so. </p> </strong> 
<br> 
<?php 
/* insert into table */ 
$selectedFlower = Request.Form("Poll2.html"); 
$inserQuery="insert into Poll2.html (response) values (" + selectedFlower + ")"; 
mysql_query($inserQuery); 
?> 
<?php 
if ( $Flower == "" ) { 
$error=true; 
echo "Please vote."; 
} else { 
echo "Your favorite flower is the: $Flower."; } 
?> 
<br><br> 
<?php 
$query="select (sum( response) /count(*)) as '% Votes'; } 
$result=mysql_query($query); 
$num=mysql_numrows($result); 
$i=0; 
echo "The Poll Results Are:" ; } 
while ($i < $num) { 
?> 
<?php 
$percentageVotes = mysql_result($result,$i,"% Votes"); 
$flowerName = mysql_result($result,$i,"response"); } 
<?php 
 
if( $flowerName = 'F1'){ 
 
$flowerName = 'Rose'; 
 
}else if($flowerName = 'F2'){ 
 
$flowerName = 'Daisy'; 
 
}else if($flowerName = 'F3'){ 
 
$flowerName = 'Tulip'; 
 
}else if($flowerName = 'F4'){ 
 
$flowerName = 'Lily'; 
 
}else if($flowerName = 'F5'){ 
 
$flowerName = 'Orchid'; 
 
}else if($flowerName = 'F6'){ 
 
$flowerName = 'Lilac'; 
 
}else if($flowerName = 'F7'){ 
$flowerName = 'Other'; 
?> 
<?php 
select (count( response) /count(*)) as '% Votes' </td><td>$flowerName </td></tr>"; } 
$i++; 
} 
?> 
<br><br> 
<input type="Submit" value="BACK" name="Submit"> 
<br> 
<p class="style5"> <strong>Thanks again for your time.</strong> </p> 
</form> 
</body> 
</html>

Recommended Answers

All 5 Replies

You've got two <?php open tags without a closing tag before the second. (See the red in your original code.)

I'm not quite sure what you're asking about, it looks like you have a decent start on your poll.

You've got two <?php open tags without a closing tag before the second. (See the red in your original code.)
I'm not quite sure what you're asking about, it looks like you have a decent start on your poll.

I'm not sure if I have properly created the database, or if the poll results will be stored and properly displayed. I need the results displayed similar to what's below:

Your favorite flower is the Rose.
There are 500 total votes.

Rose received 10% of the total vote.
Daisy received 20% of the total vote.
Other received 70% of the total vote.

I have reworked some of the code. My immediate problem, is that I'm trying to correctly code the CREATE TABLE, and INSERT statements. I am getting an error for the INSERT line.
I'm not sure that I have structured any of it right. I am still confused as to what goes where. I created a table named votes, and named the only column (it's just 1 question with 7 possible answer choices) Flower. Should I be naming each row there? I named the rows Flower1-7. Then I inserted the choices for each row. There is only 1 choice for each row. Do I need 7 insert statements? Is the SELECT statement correct?

Thanks again.Below is the troublesome code:

<?php
$query="CREATE TABLE votes (Flower);
VALUES(Flower1 not null primary key,Flower2 not null primary key,Flower3 not null primary key,Flower4 not null primary key,Flower5 not null primary key,Flower6 not null primary key,Flower7 not null primary key)" ; 
?>
<?php
INSERT INTO votes (Flower) VALUES (Daisy, Rose, Tulip, Lily, Orchid, Lilac, Other);
?>
<?php
if ( $Flower == "" ) {
$error=true;
echo "Please vote." ;
} else { 
echo "Your favorite flower is the: $Flower."; } 
?>
<br><br> 
<span class="style2">"Here are the results:"</span>
<?php
SELECT Flower FROM votes WHERE $Flower= Daisy
?>

$selectedFlower = Request.Form("Poll2.html");

Does PHP use the dot "." for refering to methods in an object. If it does thats cool, but I thought it used "->".

Create table statements need run only once per database. I have never felt the need to code them, doing most my stuff with inserts, deletes, and updates.

I think your insert query probably needs single quotes around the field names. Take a look at the manual (or Google) for MySQL and verify that, as I'm not sure.

If you're getting an error, tell use the error you're getting, it helps troubleshoot sometimes. I'm guessing you get "You have an error in your MySQL Syntax"

$selectedFlower = Request.Form("Poll2.html");

Does PHP use the dot "." for refering to methods in an object. If it does thats cool, but I thought it used "->".

Thank you again for helping>
I inserted the above code and got this error:
Fatal error: Call to undefined function: form() in /www/cgi/Poll2results.php on line 39

How do I define the function?
I need to request the results to print them. Should my PHP file be in the place of "Poll2.html"?
Did I possibly insert it in the wrong place?
I'm not sure about the "." vs "->". Actually I have no clue. I don't know what the "->" does. But, I'm going to reserach it, and when I find info, I will post it.
Could someone please show me an example of how to calculate a percentage using a variable, and how to print the results?

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.