Well what i m trying to do is that, i have a table which has loads of
questions in it, and then when i run this script qpaper.php it brings
out a question paper in front of the user. Now the user answers these
MCQ questions, and these answers should get stored in a table in my
database......
The answers go to the script called ans_store.php which stores the
result in a table called ans_stored. The thing is that on submitting
the qpaper.php file, only the last question id and the last answer
gets submitted. I m not able to run a loop of some sort, so that when
the user clicks submit, all the question ids, and answers get stored
in the table.
M giving the codes of both the files, below :
qpaper.php
<html>
<head>
<title>Question Paper of Programming</title>
</head>
<body>
<?php
$con = mysql_connect("localhost:3306","root","hellscream");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
?>
<div align="center">
<table name="Ques_head" border='0' cellspacing='10' cellpadding='10'>
<tr>
<th>Type</th>
<th>Paper</th>
<th>Section</th>
</tr>
<tr>
<td align="center">Computer Science</td>
<td align="center">Programming</td>
<td align="center">1</td>
</tr>
</table>
</div>
<br><br><br>
<?php
mysql_select_db("collegesite", $con);
$result = mysql_query("SELECT * FROM qpaper where
(paper='Programming' && section='1')");
?>
<form name="answer" action="ans_store.php" method="POST">
<?php
while($row=mysql_fetch_array($result, MYSQL_ASSOC))
{ $dd=$row['QID'];
$a1=$row['answer_a'];
$a2=$row['answer_b'];
$a3=$row['answer_c'];
$a4=$row['answer_d'];
?>
<table name="questions" border='0' cellspacing='0' cellpadding='0'>
<tr>
<td>Question ID: </td>
<td><input type="hidden" name="xyz" value="<?=$dd?>"> <?=$row['QID']?
></td>
</tr>
<tr>
<td>Question: </td>
<td><input type="hidden" name="question"><?=$row['question']?></td>
</tr>
<table name="answer_options" border='0' cellspacing='0'
cellpadding='0'>
<tr>
<td>
<input type="radio" name="ques" value="<?=$a1?>"> <?=$row['answer_a']?
>
</td>
</tr>
<tr>
<td>
<input type="radio" name="ques" value="<?=$a2?>"> <?=$row['answer_b']?
>
</td>
</tr>
<tr>
<td>
<input type="radio" name="ques" value="<?=$a3?>"> <?=$row['answer_c']?
>
</td>
</tr>
<tr>
<td>
<input type="radio" name="ques" value="<?=$a4?>"> <?=$row['answer_d']?
>
</td>
</tr>
</table>
<?php
}
?>
</table>
<tr>
<input type="submit" value="submit" name="Submit">
</tr>
</form>
<?php
mysql_close($con);
?>
</body>
</html>
ans_store.php
<html>
<head>
<title>Saving answers</title>
</head>
<body>
<?php
$con = mysql_connect("localhost:3306","root","hellscream");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("collegesite", $con);
$sql= "INSERT INTO ans_stored VALUES ('$_POST[xyz]', '$_POST[ques]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "The answers were Successfully entered";
mysql_close($con);
?>
</body>
</html>
Can any1 plzzzzzz tell me how to go about solving my problem ??
Hoping from an early reply from all you guys out there :)
Thanks in advance