Form results into a database

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]

What does the html look like for the choices?

(Just registered)
Here is my HTML code.

<html>
<div id="wrapper">
<head>
<link rel="stylesheet" type="text/css" href="project02.css" />
<title>The Vermont Web Designers Survey System </title>

</head>
<body>
<h1 align="center">A Survey about Movies</h1>
<center>
<h1 align="center">Survey</h1>
<h2>This version stores its results in a database.</h2>
<!--Startform, which uses process.php to analyize information-->
		<div id="form2">
		<form action="surveyresults.php" method="post"> 
		<div id="table2">
		<table> 
		<tr><th colspan=2>Contact me #2!</th></tr> 
		<tr><td colspan=2>Your Name:</td><td> <input type="text" name="name" size=25></td></tr>
		<tr><td colspan=2>E-mail:</td><td> <input type="text" name = "email" size=25></td></tr>
		<tr><td colspan=2>Age</td><td> <input type="text" name = "age" size=25></td></tr>
		<tr><td colspan=2>Location:</td><td> <input type="text" name = "location" size=25></td></tr>
		<tr><td></td></tr>
		</table>	
		</div>
		</div>	
        <br />	<br />	<br />	<br />	<br />	<br />	<br />	<br />	<br />	
  <table border="4" cellspacing="0" cellpadding="4">
    <tr>
      <th scope="col">&nbsp;</th>
      <th scope="col">&nbsp;</th>
      <th scope="col">Great</th>
      <th scope="col">Good</th>
      <th scope="col">OK</th>
      <th scope="col">So-So</th>
	  <th scope="col">Terrible</th>
    </tr>
	<tr>
      <td>1. The God Father Part 1</td>
      <td>Quality of Movie</td>
      <td> <input name="reply1" type="radio" value="1"> 1</td>
	  <td> <input name="reply1" type="radio" value="2"> 2 </td>
	  <td> <input name="reply1" type="radio" value="3"> 3</td>
      <td> <input name="reply1" type="radio" value="4"> 4</td>
	  <td> <input name="reply1" type="radio" value="5"> 5</td>
    </tr>
	<tr>
	<td></td>
	<td>Quality of Actor</td>
      <td> <input name="reply2" type="radio" value="1"> 1</td>
	  <td> <input name="reply2" type="radio" value="2"> 2 </td>
	  <td> <input name="reply2" type="radio" value="3"> 3</td>
	  <td> <input name="reply2" type="radio" value="4"> 4</td>
	  <td> <input name="reply2" type="radio" value="5"> 5</td>
	</tr>

	 <tr>

      <td>2. Men of Honor   </td>
      <td>Quality of Movie</td>
      <td> <input name="reply3" type="radio" value="1"> 1</td>
      <td> <input name="reply3" type="radio" value="2"> 2</td>
      <td> <input name="reply3" type="radio" value="3"> 3</td>
      <td> <input name="reply3" type="radio" value="4"> 4</td>
	  <td> <input name="reply3" type="radio" value="4"> 5</td>
    </tr>

	<tr>
	<td></td>
	<td>Quality of Actor</td>
      <td> <input name="reply4" type="radio" value="1"> 1</td>
	  <td> <input name="reply4" type="radio" value="2"> 2</td>
	  <td> <input name="reply4" type="radio" value="3"> 3</td>
      <td> <input name="reply4" type="radio" value="4"> 4</td>
	  <td> <input name="reply4" type="radio" value="4"> 5</td>
	</tr>
   <tr>
      <td>3. Three Days of the Condor    </td>
      <td>Quality of Movie</td>
      <td> <input name="reply5" type="radio" value="1"> 1</td>
      <td> <input name="reply5" type="radio" value="2"> 2</td>
      <td> <input name="reply5" type="radio" value="3"> 3</td>
      <td> <input name="reply5" type="radio" value="4"> 4</td>
	  <td> <input name="reply5" type="radio" value="4"> 5</td>
    </tr>
	<tr>
	<td></td>
	<td>Quality of Actor</td>
      <td> <input name="reply6" type="radio" value="1"> 1</td>
	  <td> <input name="reply6" type="radio" value="2"> 2</td>
	  <td> <input name="reply6" type="radio" value="3"> 3</td>
      <td> <input name="reply6" type="radio" value="4"> 4</td>
	  <td> <input name="reply6" type="radio" value="4"> 5</td>
	</tr>
  </table>
    <br><input type="submit" value="SUBMIT">
	</p>
</form>
</center>
</body>
<div id="footer">
<a href="contact.html">Contact</a>
</div>


</div>
</html>

I see 1 issue so far -

5, the value should be 5, not 4. Is that one that was getting inserted wrong?

Wow, I can’t believe I missed that. Thank you very much richei! It was actually like that for like 3 of the choices, and it was confusing me.
Now I just have to get a button to clear the scores in my database and I should be all set for this school project :slight_smile:

Not really sure what’s scored, but you could use checkboxes to select what to reset, then using a simple update query, set all the select scores to 0.

Sponsor our Newsletter | Privacy Policy | Terms of Service