Tough Time in inserting a query.
My form is having 1 question, 4 options with radio button in while loop. Questions and options are fetched from table named paper, I used array for question and answers. Below is the code I used.
<form id="form1" action="submit_answer.php" method="post">
<?php
$get_question = mysql_query("select * from paper where test_name='$test_name' ORDER BY RAND()");
$count = 0;
$sr = 1;
if(mysql_num_rows($get_question)==0)
{
echo "No Questions Found For ".$test_name;
}
else
{
while($row_question = mysql_fetch_array($get_question))
{
$id = $row_question["id"];
$question = $row_question["question"];
$option1 = $row_question["option1"];
$option2 = $row_question["option2"];
$option3 = $row_question["option3"];
$option4 = $row_question["option4"];
$answer = $row_question["answer"];
echo '<div class="question_box">
<p>'.$sr.'. '.$question.'</p>
<div style="margin:10px 0 0 10px;">
<input type="text" value="'.$std.'" name="std" />
<input type="text" value="'.$student_name.'" name="student_name" />
<input type="text" value="'.$test_name.'" name="test_name" />
<input type="text" value="'.$question.'" name="question[]" />
<input type="hidden" value="'.$answer.'" name="true_answer[]" />
<input type="radio" class="answers" name="given_answer['.$count.']" value="A" /> '.$option1.'<br/>
<input type="radio" class="answers" name="given_answer['.$count.']" value="B" /> '.$option2.'<br/>
<input type="radio" class="answers" name="given_answer['.$count.']" value="C" /> '.$option3.'<br/>
<input type="radio" class="answers" name="given_answer['.$count.']" value="D" /> '.$option4.'<br/>
<input checked="checked" type="radio" class="answers" name="given_answer['.$count.']" value="NONE" style="display:none;" />
</div>
</div>';
$count++;
$sr++;
}
}
?>
<button class="stdbtn btn_black" style="opacity: 1;" type="button" onclick="submit_exam();">Submit</button>
</form>
Now, submit_answer.php page is for submitting all questions and answers with student std, student name, test name, questions and answers. Below is the code I used:
foreach($_POST as $key => $value)
{
echo $key;
echo "<br />";
print_r($value);
echo "<br />";
echo "<br />";
mysql_query("insert into student_answer($key) values($value)");
echo mysql_error();
}
It does insert std of the student but not the rest of the values and gives me error.
**Unknown column 'Array' in 'field list'**
Table fields are:
+------++--------------++-----------++----------++--------------+
| std || student_name || test_name || question || given_answer |
+------++--------------++-----------++----------++--------------+
Kindly help me out in above query. Thanks in Advance!! 🙂
LOL I got my own answer, query will be as per the below: