The following code is my surveyresults.php page. The problem is that when you select a radio button(5 choices), it will store it in the database as a different radio button. Similar to an off by one bug, but it only does it for like the last 4 radio buttons. Any help/coding improvement suggestions are appreciated.
[php]<?php
function make_header()
{
print ’
Results of a Survey about Movies
Results of a Survey about Movies
';
}
function capture_data()
{
for ($i=1; $i<=6; $i++)
{
$handle=“reply”.$i;
$reply[$i]=$_POST[$handle];
}
return $reply;
}
function update_database($newdata)
{
$connection = mysql_connect(“localhost”,“me”,“me”) or die(‘Can not connect’);
mysql_select_db(“movies”, $connection) or die(‘Can not connect to Database’);
for ($rownumber=1; $rownumber<=8; $rownumber++)
{
$myquery= "SELECT * FROM movies where pid=".$rownumber;
$result = mysql_query ($myquery, $connection) or die(mysql_error());
$row = mysql_fetch_array($result); /* grab one row of the table */
for ($j=1; $j<=5; $j++)
{
$scorehandle="score".$j;
$count[$j]=$row[$scorehandle];
}
/* Now, increment the count, based on the current survey results\ */
$score=$newdata[$rownumber];
if ($score>=1 && $score<=6) /* Safety check */
{
$count[$score]++;
/* Now, write that row information back into the database */
$putquery="UPDATE movies SET score".$score." = ".$count[$score]." where pid=".$rownumber;
/*show what is being written back into the database*/
/*print $putquery;*/
$result= mysql_query ($putquery, $connection);
}
} /* end of the row-by-row update loop */
} /* update_database */
function display_database( )
{
$connection = mysql_connect(“localhost”,“me”,“me”);
mysql_select_db(“movies”, $connection);
$myquery= "SELECT * FROM movies";
$result = mysql_query ($myquery, $connection);
print "<table border=0 cellpadding=4>";
print "<tr><th></th><th></th><th>Great</th><th>Good</th><th>OK</th><th>So-So</th><th>Terrible</th></tr>";
while ($row = mysql_fetch_array($result))
{
// Print out each element in $row, that is, print the values of
// the attributes
/* put something here to rints the row headers */
/* this starts the reply */
print "<td align=center>Quality of Movie</td>";
print "<td align=center>Quality of Actor</td>";
print "<td align=center>".$row['score1']."</td>";
print "<td align=center>".$row['score2']."</td>";
print "<td align=center>".$row['score3']."</td>";
print "<td align=center>".$row['score4']."</td>";
print "<td align=center>".$row['score5']."</td>";
print "</tr> \n";
}
print “”;
} /* display_database */
// ############### MAIN PROGRAM ######################
make_header();
$results=capture_data();
update_database($results);
display_database();
//### Now offer choices for what happens next
print ’
<form action="surveyzero.php" method="POST">
<input type="submit" value="CLEAR THE DATABASE">
</form></p>
</body>
</html>
';
?>
[/php]