| | |
Need Help with Code
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2006
Posts: 4
Reputation:
Solved Threads: 0
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.
[php]
</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>
[/php]
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.
PHP Syntax (Toggle Plain Text)
<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>
[php]
</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>
[/php]
•
•
Join Date: Jul 2004
Posts: 494
Reputation:
Solved Threads: 21
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 quite sure what you're asking about, it looks like you have a decent start on your poll.
www.uncreativelabs.net
Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
•
•
Join Date: Oct 2006
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
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.
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]
<?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
?>
[/php]
•
•
Join Date: Jul 2004
Posts: 494
Reputation:
Solved Threads: 21
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"
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"
www.uncreativelabs.net
Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
•
•
Join Date: Oct 2006
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
$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 "->".
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?
![]() |
Similar Threads
- Code 19 Registry Error (Windows NT / 2000 / XP)
- Why won't this code work? (VB.NET)
- Need help with DirectX code (C)
- Tutorials & Code Submissions - Questions? (DaniWeb Community Feedback)
- Some Basic Code Hopefully (Help Needed) (HTML and CSS)
Other Threads in the PHP Forum
- Previous Thread: How to insert PHP in HTML language?
- Next Thread: Storing dynamic form values in Arrays for display & insert
| Thread Tools | Search this Thread |
ajax apache api array beginner binary broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link list login loop mail mediawiki menu mlm multiple mycodeisbad mysql number oop paypal pdf php phpincludeissue phpmyadmin problem query radio random recursion regex remote script search seo server sessions sms soap source sp space speed sql subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign websphere white xml youtube





