更新我的帖子6/15/14我的新问题是我在每个文本输入旁边都有复选框,它会指示(如果选中)文本框中的文本是正确的答案。但是,当我更改选中哪个复选框的特定答案时,它没有正确保存。
这是我的完整更新代码
<?php
//if save was clicked
if(isset($_POST['saveit']))
{
$tmodnameid = $_POST['tmodnameid'];
$tmodnameid = mysqli_real_escape_string($dbc, $tmodnameid);
$quizpercent = $_POST['quizpercent'];
$quizpercent = mysqli_real_escape_string($dbc, $quizpercent);
mysqli_query($dbc, "UPDATE training_quizs set tmoduleid='$tmodnameid', quiz_passingpercent='$quizpercent'
WHERE quizid='$quid'");
//lets delete all questions and answers since we are re-saving them
$q = mysqli_query($dbc, "SELECT qarowid FROM training_quizs_questions WHERE quizid='$quid'");
while($qqr = mysqli_fetch_array($q))
{
$qarowid = $qqr['qarowid'];
mysqli_query($dbc, "DELETE from training_quizs_answers WHERE qarowid='$qarowid'");
}
mysqli_query($dbc, "DELETE from training_quizs_questions WHERE quizid='$quid'");
$n = 0;
$newquestion = $_POST['newquestion'];
foreach($newquestion as $aquestion)
{
mysqli_query($dbc, "INSERT into training_quizs_questions(quizid, qquestion)
VALUES('$quid','$aquestion')");
$questionrowid = mysqli_insert_id($dbc);
//lets save each of this questions answers
//save first potential answer for this question
$answera = $_POST['answera'][$n];
$isanswera ='';
$isanswera = trim($_POST['isanswera'][$n]);
if(!empty($isanswera))
{
$isanswera ='yes';
$firstsql = mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer)
VALUES('$questionrowid','$answera','$isanswera')");
}else{
$isanswera = '';
$firstsql = mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer)
VALUES('$questionrowid','$answera',' ')");
}
//save second potential answer for this question
$answerb = $_POST['answerb'][$n];
$isanswerb='';
$isanswerb = trim($_POST['isanswerb'][$n]);
if(!empty($isanswerb))
{
$isanswerb ='yes';
mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer)
VALUES('$questionrowid','$answerb','$isanswerb')");
}else{
$isanswerb = '';
mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer)
VALUES('$questionrowid','$answerb',' ')");
}
//save third potential answer for this question
$answerc = $_POST['answerc'][$n];
$isanswerc = trim($_POST['isanswerc'][$n]);
if(!empty($isanswerc))
{
$isanswerc ='yes';
mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer)
VALUES('$questionrowid','$answerc','$isanswerc')");
}else{
$isanswerc = '';
mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer)
VALUES('$questionrowid','$answerc',' ')");
}
//save fourth potential answer for this question
$answerd = $_POST['answerd'][$n];
$isanswerd = trim($_POST['isanswerd'][$n]);
if(!empty($isanswerd))
{
mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer)
VALUES('$questionrowid','$answerd','yes')");
}else{
mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer)
VALUES('$questionrowid','$answerd',' ')");
}
//save fifth potential answer for this question
$answere = $_POST['answere'][$n];
$isanswere = trim($_POST['isanswere'][$n]);
if(!empty($isanswere))
{
mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer)
VALUES('$questionrowid','$answere','yes')");
}else{
mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer)
VALUES('$questionrowid','$answere',' ')");
}
//save sixth potential answer for this question
$answerf = $_POST['answerf'][$n];
$isanswerf = trim($_POST['isanswerf'][$n]);
if(!empty($isanswerf))
{
mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer)
VALUES('$questionrowid','$answerf','yes')");
}else{
mysqli_query($dbc, "INSERT into training_quizs_answers(qarowid, answer, isanswer)
VALUES('$questionrowid','$answerf',' ')");
}
$n++;
}
//get rid of any blank rows that were created
mysqli_query($dbc, "DELETE from training_quizs_answers WHERE answer=''");
echo "<div class='alert alert-success'>
<h4>Success!</h4>
You successfully setup a new quiz from the training module.
</div>";
}
//lets get the questions
$qsql = mysqli_query($dbc, "SELECT * FROM training_quizs WHERE quizid='$quid' LIMIT 1");
$qsqlrow = mysqli_fetch_array($qsql);
extract($qsqlrow);
$quiz_createdon = date('m/d/Y', strtotime($quiz_createdon));
?>
<form method='POST' action='' enctype='multipart/form-data' name='form1' id='form1'>
<div class="row-form clearfix">
<div class="span4">
Training Module Name
<select name='tmodnameid'>
<?php
//lets select all training modules that have not quiz yet
$atlistsql = mysqli_query($dbc, "SELECT tmoduleid, tmodule_name FROM training_modules WHERE
tmoduleid='$tmoduleid' LIMIT 1");
$atlistrow = mysqli_fetch_array($atlistsql);
$tmoduleid = $atlistrow['tmoduleid'];
$tmodule_name = $atlistrow['tmodule_name'];
echo "<option value='$tmoduleid'>$tmodule_name</option>";
$tlistsql = mysqli_query($dbc, "SELECT tmoduleid, tmodule_name FROM training_modules WHERE tmoduleid !='$tmoduleid'
ORDER by tmodule_name desc");
while($tlistrow = mysqli_fetch_array($tlistsql))
{
$tmoduleid = $tlistrow['tmoduleid'];
$tmodule_name = stripslashes($tlistrow['tmodule_name']);
echo "<option value='$tmoduleid'>$tmodule_name</option>";
}
?>
</select>
</div>
<div class="span2">
Percent needing to pass<br />
<input type='text' name='quizpercent' style='width:50px' maxlength='3' value='<?php echo "$quiz_passingpercent";?>'></strong>%</strong>
</div>
</div>
<div class="row-form clearfix">
<?php
$question_num = 1;
//lets list all the questions
$qsql = mysqli_query($dbc, "SELECT * FROM training_quizs_questions WHERE quizid='$quid' ORDER BY qarowid asc");
while($qrow = mysqli_fetch_array($qsql))
{
extract($qrow);
$qquestion = stripslashes($qquestion);
echo "<strong>$question_num)</strong><br /><textarea name='newquestion[]'>$qquestion</textarea><br />
<br /><strong>Question $question_num Potential Answers</strong><br />";
//lets get all the potential answers
$numpotentialanswers = 0;
$letter = 'a';
$displayletter = 'A';
$qaaql = mysqli_query($dbc, "SELECT * FROM training_quizs_answers WHERE qarowid='$qarowid'
ORDER by answerrow ASC");
while($qarow = mysqli_fetch_array($qaaql))
{
extract($qarow);
$answer = stripslashes($answer);
$numpotentialanswers++;
if($isanswer =='yes')
{
$checked ='checked';
}
else
{
$checked ='';
}
$answerletter[] = "answer$letter";
$isanswerletter[] = "isanswer$letter";
?>
<div style='float:left; padding-right:4px'> <?php echo "$displayletter";?>)
<input type='text' <?php echo "name='answer" . $letter. "[]'"; ?> value='<?php echo "$answer";?>' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswer<?php echo "$letter";?>[]' value='yes' <?php echo "$checked";?>>
</div><div style='clear:both'></div>
<?php
$letter++;
$displayletter++;
}
//lets display the additional potential answer rows
if($numpotentialanswers == 1)
{
echo "<div style='float:left; padding-right:4px'> B)
<input type='text' name='answerb[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswerb[]' value='yes'>
</div><div style='clear:both'></div>
<div style='float:left; padding-right:4px'> C)
<input type='text' name='answerc[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswerc[]' value='yes'>
</div><div style='clear:both'></div>
<div style='float:left; padding-right:4px'> D)
<input type='text' name='answerd[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswerd[]' value='yes'>
</div><div style='clear:both'></div>
<div style='float:left; padding-right:4px'> E)
<input type='text' name='answere[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswere[]' value='yes'>
</div><div style='clear:both'></div>
<div style='float:left; padding-right:4px'> F)
<input type='text' name='answerf[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswerf[]' value='yes'>
</div><div style='clear:both'></div>";
}
if($numpotentialanswers == 2)
{
echo "<div style='float:left; padding-right:4px'> C)
<input type='text' name='answerc[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswerc[]' value='yes'>
</div><div style='clear:both'></div>
<div style='float:left; padding-right:4px'> D)
<input type='text' name='answerd[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswerd[]' value='yes'>
</div><div style='clear:both'></div>
<div style='float:left; padding-right:4px'> E)
<input type='text' name='answere[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswere[]' value='yes'>
</div><div style='clear:both'></div>
<div style='float:left; padding-right:4px'> F)
<input type='text' name='answerf[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswerf[]' value='yes'>
</div><div style='clear:both'></div>
";
}
if($numpotentialanswers == 3)
{
echo "<div style='float:left; padding-right:4px'> D)
<input type='text' name='answerd[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswerd[]' value='yes'>
</div><div style='clear:both'></div>
<div style='float:left; padding-right:4px'> E)
<input type='text' name='answere[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswere[]' value='yes'>
</div><div style='clear:both'></div>
<div style='float:left; padding-right:4px'> F)
<input type='text' name='answerf[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswerf[]' value='yes'>
</div><div style='clear:both'></div>
";
}
if($numpotentialanswers == 4)
{
echo "<div style='float:left; padding-right:4px'> E)
<input type='text' name='answere[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswere[]' value='yes'>
</div><div style='clear:both'></div>
<div style='float:left; padding-right:4px'> F)
<input type='text' name='answerf[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswerf[]' value='yes'>
</div><div style='clear:both'></div>
";
}
if($numpotentialanswers == 5)
{
echo "<div style='float:left; padding-right:4px'> F)
<input type='text' name='answerf[]' style='width:400px'><br /><br />
</div>
<div style='float:left'>
is answer <input type='checkbox' name='isanswerf[]' value='yes'>
</div><div style='clear:both'></div>
";
}
$question_num++;
}
?>
<br />
<input type="button" onclick="addInput()" name="add" value="Add Question" /><br />
<div id='text'></div>
</div>
<div class="footer tar">
<input type='submit' name='saveit' value='Save' is='submit' class='btn'></form>
</div>
</div>
答案 0 :(得分:0)
您的所有表单字段都没有数组names[]
,因此访问$_POST['answera'][$n]
只会提取一个字母。