Dynamic form in PHP not processing correctly
- by user1497265
My last question regarding this suggested I incorporate AJAX with PHP. However, I really wanted to try PHP exclusively for this project, and I seem to have made it about 95% there. I just need help on this one issue.
Here's a quick background. My project requires a dynamic form to be populated with a max limit of 10 questions. Each form contains one question, one question number, and a text field. Students would go on and answer the questions. This is all driven by a database table (obviously), and when a question gets answered correctly, it will close and the next question in line will appear. There will always be 10 questions on the page. Here's how the coding looks, and it works perfectly.
<?
$rt = mysql_query("SELECT * FROM The_Questions WHERE Status='Open' ORDER BY 'Number' LIMIT 10");
while ($row = mysql_fetch_array($rt)) {
$number=$row[0];
$category = $row[1];
$question=$row[2];
$points=$row[4];
$_SESSION['number'] = $number;
?>
<form action="processor.php" method="post" class="qForm">
<div class="questionCell">
<div class="question"><? echo $number; echo $question ?></div>
<div class="answer">Answer: <input class="inputField" name="q1" type="text" size="40" maxlength="40" />
<input name="HHQuestion" value="Submit" type="submit" /></div>
</div>
</form>
<?
}
?>
The questions appear as they should, in the correct order, and the correct limit. Everything seems to be looking fine until a question gets answered and gets processed through the processor.php action. First here's the code to the processor.php file:
<?php
session_start();
if(isset($_POST["HHQuestion"])){
$dbhost = 'localhost';
$dbname = 'localhost';
$dbuser = 'localhost';
$dbpass = 'localhost';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $conn);
{
$number1 = $_SESSION['number'];
$answer=$_POST['q1'];
$sql="SELECT * FROM The_Questions WHERE Number='$number1'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$question = $row[2];
echo $question .'<br>';
echo $number1.'<br>';
echo $answer;
}
}
?>
This is NOT live yet, and for testing purposes I'm echoing the question, question number, and answer (as you can see). What's happening is that the $question and $number1 displays the last question in the array (the $answer displays correctly, meaning it displays whatever was written in the dynamic form).
Can anyone tell me why that is? If I change the LIMIT number to 20, the processor.php action will display the 20th question and number, even if I was answering question 8, for example, in the dynamic form. Again, the dynamic forms are being displayed correctly, and are numbered correctly. For some unknown reason to me, the action - processor.php - is grabbing the last question in the array.
Any ideas on what I'm doing wrong? I'm hoping it's a simple code change that I'm overlooking. Thanks in advance guys!