伙计们,我是新来的人,任何人都可以帮助我...我的问题是我正在使用php和ajax进行在线测验考试。问题是我正在向数据库中插入问题ID,所以有人可以帮助我解决这个问题...我给你看我的密码..
<script>
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var stu_ans=$(this).val();
var stu_id ="<?php echo $_GET['sid'];?>";
var q_id = $('#qname').val();
$.ajax({
url:"insert1.php",
method:"POST",
data:{stu_ans:stu_ans, stu_id:stu_id,q_id:q_id},
success:function(data){
$('#result').html(data);
}
});
});
});
</script>
<script>
$('.cont').addClass('hide');
$('#question_splitter_1').removeClass('hide');
$(document).on('click','.next',function(){
last=parseInt($(this).attr('id')); console.log( last );
nex=last+1;
$('#question_splitter_'+last).addClass('hide');
$('#question_splitter_'+nex).removeClass('hide');
});
$(document).on('click','.previous',function(){
last=parseInt($(this).attr('id'));
pre=last-1;
$('#question_splitter_'+last).addClass('hide');
$('#question_splitter_'+pre).removeClass('hide');
});
</script>
<style>
input[type="radio"] {
-ms-transform: scale(1.5); /* IE 9 */
-webkit-transform: scale(1.5); /* Chrome, Safari, Opera */
transform: scale(1.5);
}
</style>
<div class="container-fluied">
<div class="col-xs-12">
<hr>
<form role="form" id='formid' method="post" action="result.php?s_id=<?php echo $_GET['sid'];?>">
<?php
$number_question = 1;
$row = mysqli_query( $con, "select * from question where subject_id='$category' ORDER BY RAND()");
$rowcount = mysqli_num_rows( $row );
$remainder = $rowcount/$number_question;
$i = 0;
$j = 1; $k = 1;
?>
<h4>Total Question:-<?php echo $rowcount;?></h4>
<?php while ( $result = mysqli_fetch_assoc($row) ) {
if ( $i == 0) echo "<div class='cont' id='question_splitter_$j'>";?>
<div id='question<?php echo $k;?>' >
<p class='questions' id="qname<?php echo $j;?>" style="font-size:20px;font-weight:bold;color:green"> (<?php echo $k?>).<?php echo $result['que'];?></p>
<input type="hidden" value="<?php echo $result['que'];?>" name="qname<?php echo $j;?>" id="qname">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>(A)<?php echo trim($result['o1']);?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o1'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'></td>
</tr>
<tr>
<td>(B)<?php echo $result['o2'];?>
</td>
<td colspan="5"><input type="radio" value="<?php echo $result['o2'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/></td>
</tr>
<tr>
<td>(C)<?php echo $result['o3'];?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o3'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
</td>
</tr>
<tr>
<td>(D)<?php echo $result['o4'];?></td>
<td colspan="5"><input type="radio" value="<?php echo $result['o4'];?>" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
</td>
</tr>
</tbody>
</table>
</div>
<input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
<br/>
</div>
<?php
$i++;
if ( ( $remainder < 1 ) || ( $i == $number_question && $remainder == 1 ) ) {
echo "<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
echo "</div>";
} elseif ( $rowcount > $number_question ) {
if ( $j == 1 && $i == $number_question ) {
echo "<button id='".$j."' class='next btn btn-success' type='button'>Next</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $k == $rowcount ) {
echo " <button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
<button id='".$j."' class='next btn btn-success' type='submit'>Finish</button>";
echo "</div>";
$i = 0;
$j++;
} elseif ( $j > 1 && $i == $number_question ) {
echo "<button id='".$j."' class='previous btn btn-success' type='button'>Previous</button>
<button id='".$j."' class='next btn btn-success' type='button' >Next</button>";
echo "</div>";
$i = 0;
$j++;
}
}
$k++;
} ?>
</form>
</div>
</div>
<?php
session_start();
include('db_connect.php');
include('db_connection.php');
$stu_id=$_SESSION['sid'];
echo $gender=$_POST['stu_ans'];
echo $que=$_POST['q_id'];
$q=mysqli_query($con,"SELECT * FROM question where id='$que'");
$ro=mysqli_fetch_array($q);
//$que=$ro['id'];
$date=date('d/m/y');
$right_ans=$ro['right_ans'];
////////////////////////Ajax Data Saved//////////////////////////////
$q1=mysqli_query($con,"SELECT * FROM student_ans where q_id='$que' AND stu_id='$stu_id'");
$ro1=mysqli_num_rows($q1);
if($ro1==''){
$sql = mysqli_query($con,"INSERT INTO student_ans(stu_ans,q_id,stu_id,date,right_ans) value('$gender','$que','$stu_id','$date','$right_ans')");
}else{
$sql1 = mysqli_query($con,"update student_ans set stu_ans='$gender' where q_id='$que' AND stu_id='$stu_id'");
}
?>
此代码的工作方式与单击单选按钮时一样,因此ajax函数可以正常工作并将答案保存到数据库中,但是当我将答案插入带有答案插入问题ID的数据库中时,我希望这样做