/    Sign up×
Community /Pin to ProfileBookmark

Simple Poll Script – New to PHP – What next?

Hello:

I’m attempting to learn PHP and I’m writing a simple poll script. Thus far, I was able to create the form and update the database each time sometime submits the form with their choice for an answer.

The problem I’m having now is trying to display the results of the poll from the database.

My select query is working because I’m getting the correct number of rows from the table.

I have no idea where to go from there. Can someone help me in trying to display the results of the poll?

I tried the foreach structure and a “while” statement. All I got was a blank table.

My feeling is that this may be quite simple and I’m missing something basic. At this point, I’m not sure what that is. Can someone help me out and point me in the right direction?

Thank you so very much in advance.


——————————————-

Here are the details:

Table fields: name and tally

My form syntax is:

<form method=”post” action=”poll.php”>
<p><font size=”2″ face=”Verdana”>What is your favorite color?</font></p>
<font face=”Verdana”><input type=”radio” name=”color[]” value=”blue”></font><font size=”2″ face=”Verdana”>Blue<br></font>
<font face=”Verdana”><input type=”radio” name=”color[]” value=”green”></font><font size=”2″ face=”Verdana”>Green<br></font>
<font face=”Verdana”><input type=”radio” name=”color[]” value=”pink”></font><font size=”2″ face=”Verdana”>Pink<br></font>
<font face=”Verdana”><input type=”radio” name=”color[]” value=”purple”></font><font size=”2″ face=”Verdana”>Purple<br></font>
<font face=”Verdana”><input type=”radio” name=”color[]” value=”red”></font><font size=”2″ face=”Verdana”>Red<br></font>
<font face=”Verdana”><input type=”radio” name=”color[]” value=”yellow”></font><font size=”2″ face=”Verdana”>Yellow</font><br><br>

<input type=”submit” name=”choice” value=”Submit”>
</form>

My poll script is:

<?php

//connect to server and select database
$conn = mysql_connect(“mysql.addr.com”, “summitweb”, “summitdb”) or die(mysql_error());
mysql_select_db(“summitweb”,$conn) or die(mysql_error());

foreach ($color as $value){
mysql_query(“UPDATE color SET tally=tally + 1 WHERE name LIKE ‘$value'”);
}

$result = mysql_query(“SELECT name, tally FROM color”) or die(mysql_error());

$num_rows = mysql_num_rows($result);

$display_block = “<font size=”2″ face=”Verdana”>$num_rows</font>
<table width=”457″ border=”1″>
<tr>
<td width=”170″><font size=”2″ face=”Verdana”></font></td>
</tr>

</table>”;

?>
<html>
<head>
<meta http-equiv=”Content-Language” content=”en-us”>

<title></title>
<link href=”style.css” rel=”stylesheet” type=”text/css”>
</head>
<body>

<?php echo $display_block; ?>
</body>
</html>

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@sciguyryanSep 19.2004 — Well, you'll probably have to extract the information using [url=http://www.php.net/mysql_fetch_array]mysql_fetch_array()[/url]

And then you can use the While structure to do something like this:

Demo:
[code=php]
<?php
$SQL = "SELECT * FROM Polls";
$Result = mysql_query($SQL);
while ($Poll = mysql_fetch_array($Result)){
$Text = "<tr><td>" . $Poll["Name"] . "</td>"
$Text .= "<td>" . $Poll["Votes"] . "</td></tr>";
}

echo "<table>";
echo "$Text";
echo "</table>";
?>
[/code]



Hope that helps,

RyanJ
×

Success!

Help @summit310 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...