Hello; I am trying to establish an online exam system. A teacher writes questions which are recorded into a Mysql table with php. Simultaneously a new, corresponding answer column is added to students table for each new question. I managed to create the question - answer form. However, I don't know how to arrange the php page which this form is posted. Below is the answer form. "ss" stands for question number $result = mysql_query("SELECT * FROM question WHERE kod='$kod' ORDER BY id asc") or die(mysql_error()); echo "<table border='1'><form name='answers' method='post' action='get_answers.php'>"; echo "<input type='hidden' name='kod' value='", $kod, "' />"; while($row = mysql_fetch_array( $result )) { $question = $row['question']; echo "<tr><td>", $row['ss'], "- ", $question, "</td></tr>", "<tr><td><input type='radio' name='answer_", $row['ss'], "' value='", $row['a'], "' />", $row['a'], " <input type='radio' name='answer_", $row['ss'], "' value='", $row['b'], "' />", $row['b'], " <input type='radio' name='answer_", $row['ss'], "' value='", $row['c'], "' />", $row['c'], "<input type='radio' name='answer_", $row['ss'], "' value='", $row['d'], "' />", $row['d'], "<input type='radio' name='answer_", $row['ss'], "' value='", $row['e'], "' />", $row['e'], "<input type='hidden' name='ss' value='", $row['ss'], "' /></td></tr>"; } echo "<tr><td><input type='submit' /></td></tr></form>"; echo "</table>"; PHP: In the get_answers.php page I can't use the traditional $answer_1 = $_POST['answer_1'] PHP: method because I don't know how many answers there will be as there will be many different exams. This is a problem especially when I try to update the student rows in accordance with their answers because when more than enough number of columns are specified in the UPDATE clause, an error appears, saying "column answer_18 doesn't exist". Now, is it possible to determine the "answers_#" columns for each exam and write the update clause accordingly each time new answers are given?
Let me simplify the question: The number of questions and the corresponding answers varies from exam to exam. The problem is how to set the UPDATE command, or the SET clause in it. UPDATE table_name SET answer_1 = '$answer_1', answer_2 = '$answer_2' PHP: and so on. How long that will go depends on the number of answers. I can get the number but I don't know how to set it into that clause.
I don't think it is good idead to keep answers in columns. maybe try structure like: StudentAnswers Table id, student_id, exam_id, question_id, answer_id Or if you want to stick to your method, try to use query to check if column exists: SELECT [column_name] FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=’[table_name]’ PHP:
you want to check how many answers there will be. Easy solution it doesn't matter if you have $_POST var filled, it just fill blank field and then you will select only non-blank fields It's little bit strange but I hope you will get it
I think I didn't express myself properly Let me try again. There will be one answer for each question. Answers by different students will be stored in different rows which include their numbers. So, the rows are already there. Therefore I need UPDATE. And the questions are stored in another table. All I want is to update the answer rows. So, how should I write the update command. for ($i = 1; $i<8;$i++) { ${"answer_".$i} = $_POST['answer_'.$i]; mysql_query("UPDATE students SET ?????????????????????????????? WHERE name='$name' and kod = '$kod'") or die(mysql_error()); } PHP: