Hi,
I have a problem adding the values of an array into a mysql database. The thing is
I have two loops and if I add the INSERT in one of the then the other one gives the wrong value. But if I echo inside each loop it gives the right values.
Here is my code:
Form page:
<form action="student_assignment_submit.php" method="post"
name="details" enctype="multipart/form-data"
onsubmit="javascript: return checkPass();"><?php
//echo $row['course_outline_uid_fk'];
$sql = "SELECT * FROM live_course WHERE uid = {$_REQUEST['course']}";
$results = mysql_db_query($dbName,$sql,$connect);
// if (mysql_num_rows($results) != 0){
$row = mysql_fetch_assoc($results);
//echo $row['course_outline_uid_fk'];
$countquestion = 0;
$ass_sql = "SELECT * FROM assignment_questions WHERE course_uid = {$row['course_outline_uid_fk']}";
$ass_results = mysql_db_query($dbName,$ass_sql,$connect);
//if (mysql_num_rows($results) != 0){
while ($row_ass = mysql_fetch_assoc($ass_results)) {
echo $row_ass['uid'];
$countquestion++;
?> <input type="hidden" name="course_id[]"
value="<?=$row['course_outline_uid_fk']?>" /> <input
type="hidden" name="student_id[]" value="<?=$uid?>" /> <input
type="hidden" name="question_id[]" value="<?=$row_ass['uid']?>" />
<table>
<tr bgcolor="#c3c3c3">
<td class="style6">Question <?=$countquestion?></td>
</tr>
<tr>
<td><?= $row_ass['question']?></td>
</tr>
<tr>
<td><?php
if ($row_ass['answer1'] == '') {
echo '';
}else{
$countanswer++;
?>
<ol>
<li type="a"><input type="radio"
name="answer[<?=$countquestion?>]" value="answer1" /> <?=$row_ass['answer1']?></li>
<?php }?>
<?php
if ($row_ass['answer2'] == '') {
echo '';
}else{
$countanswer=$countanswer2;
$countanswer2;
?>
<li type="a"><input type="radio"
name="answer[<?=$countquestion?>]" value="answer2" /> <?=$row_ass['answer2']?></li>
<?php }?>
<?php
if ($row_ass['answer3'] == '') {
echo '';
}else{
$countanswer2=$countanswer3;
$countanswer3++;
?>
<li type="a"><input type="radio"
name="answer[<?=$countquestion?>]" value="answer3" /> <?=$row_ass['answer3']?></li>
<?php }?>
<?php
if ($row_ass['answer4'] == '') {
echo '';
}else{
?>
<li type="a"><input type="radio"
name="answer[<?=$countquestion?>]" value="answer4" /><?=$row_ass['answer4']?></li>
<?php }?>
</ol>
</td>
</tr>
</table>
<?php }?>
<table>
<tr>
<td align="center" colspan="3"><br />
<input type="image" src="images/submit.png" alt="Save"
onclick="return checkform(details)" /></td>
</tr>
</table>
</form>
Submit page:
<?php
require_once("settings.inc.php");
// require_once("student_session.inc.php");
session_start();
for($d=0;$d<=count($_POST[answer]);$d++){
$answer = $_POST[answer][$d];
//echo $answer;
$ids = $_REQUEST['question_id'];
foreach ($ids as $value) {
//echo $value;
//
$sql = "INSERT INTO student_score(answer) VALUES ('$answer')";
$results = mysql_db_query($dbName,$sql,$connect);
}
}
if (!$results) {
$_SESSION['msg'] = "Could not save information, Please try again";
header("Location:student_assignment.php");
die;
}
$_SESSION['msg'] = "Question successfully created";
header("Location:student_assignment.php");
die;
?>
If someone can please help me, It would be really nice.