0

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!

2
  • omg, can be truly specific. it will be hazard my nervous of brain system. Maybe you should try without brutal $_SESSION. Be moderate sligtly. Omg, this is VULNEARBLE, NOT SECURE CODE, SQL INJECTION, hard to fix, clean your style. Try writing code on a new clean project. Why you should use session for simple choices? Oct 14, 2012 at 3:45
  • As an aside, I think it would be "better" (ie clearer, less prone to maintenance/bugs) to SELECT question, answer FROM The_Questions WHERE Number='$number1' (and sanitize $number to ensure it is just a number (and not injected code), then access $row['question'] and $row['answer'] rather than using number indexes.
    – Sepster
    Oct 14, 2012 at 3:54

1 Answer 1

1

Please read about SQL Injections. This is very important.

Also, please note:

  1. What are the curly braces after mysql_select_db for in your processor.php?
  2. Do you have session_start() in the other file or only in processor.php?
  3. You are overwriting your $_SESSION['number'] in your while() loop, the last entry that will be placed in the variable is the last record in your $rt = mysql_query("SELECT * FROM The_Questions WHERE Status='Open' ORDER BY 'Number' LIMIT 10"); So, it's logical for it to contain the 20th question when you change the limit to 20.

What you can do:

  1. Add this to your <form> and stop using the $_SESSION:

    <input type="hidden" name="num" value="<?php echo $number; ?>" />

  2. Then, in your loop, change $number1 = $_SESSION['number']; to:

    $number1 = $_POST['num'];

  3. Try not to cause confusion with the variable names you use. You used $q1 for the answer, and you are using $number1 for the item number. Using more descriptive variables will help you later as you debug.

1
  • That worked perfectly!!! I rarely use the hidden field in a form. Thank you very much! Oct 14, 2012 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.