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>