我是php web app dev的新手。我正在设计一个在线资质测试。但我遇到了一些我无法继续前进的问题。这是
以下是我的代码:
//test.php
<?php
session_start();
// th value of q is given as 1 from the start quiz button of the users home page
if (isset($_GET['q'])) {
$number = (int)$_GET['q'];
}else{
header('Location : index.php');
}
if(!isset($_SESSION['user_session']))
{
header("Location: ../login.html");
}
//connection to the db
include_once '../dbconfig.php';
$stmt = $db_con->prepare("SELECT * FROM tbl_admin WHERE aid=:uid");
$stmt->execute(array(":uid"=>$_SESSION['user_session']));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
$qr = $db_con->prepare("SELECT * FROM questions ");
$qr->execute();
$count = $qr->rowCount();
$qrow=$qr->fetch(PDO::FETCH_ASSOC);
$qst = $db_con->prepare("SELECT * FROM questions where quesNo='$number' ");
$qst->execute();
$qstrow=$qst->fetch(PDO::FETCH_ASSOC);
?>
<body>
<header id="header">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"><div id="hms">00:05:00</div></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
</header>
<div class="item active">
<?php
//Code to process score(saved is session variable) and move to the next question
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$ans=$_POST['ans'];
$number=$_POST['number'];
$next = $number+1;
if (!isset($_SESSION['score'])){
$_SESSION['score'] = '0';
}
$qrs = $db_con->prepare("SELECT * FROM questions ");
$qrs->execute();
$counts = $qrs->rowCount();
$sqlans= "SELECT * FROM answers where quesNo='$number' and right_ans='1'";
foreach ($db_con->query($sqlans) as $rowsans) {
if($ans==$rowsans['ans_id']){
$_SESSION['score']++;
}
if($number==$count){
header('location:final.php');
exit();
}else{
header('location:test.php?q='.$next);
}
}
}
?>
<div class="containerx" style="height:530px;">
<div class="image" style="width:100%">
<div class="main">
<hl> Question <?php echo $number ?> of <?php echo $count; ?></hl>
<div class="test">
<form method="post" action="">
<table>
<tr>
<td coLspan=”2”>
<h3>Que <?php echo $qstrow['quesNo']; ?>: <?php echo $qstrow['ques']; ?></h3>
</td>
</tr>
<?php
$sqla= "SELECT * FROM answers where quesNo='$number'";
foreach ($db_con->query($sqla) as $rowsa) {
?>
<tr>
<td>
<input type="radio" name="ans" value=" <?php echo $rowsa['ans_id']; ?>"/> <?php echo $rowsa['ans']; ?>
</td>
</tr>
<?php } ?>
<tr>
<td>
<input type="submit" id="btn_next" name="submit" value="Next Question"/>
<input type="hidden" name="number" value="<?php echo $number ?>"/>
</td>
</tr>
</table>
<!--<div class="caption page-carousel pull-left">-->
</div>
<div class="site-full-width darkbluecolor">
<div class="container">
</div>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
function count(){
var startTime = document.getElementById('hms').innerHTML;
var pieces= startTime.split(":");
var time = new Date();
time.setHours(pieces[0]);
console.log( time.setMinutes(pieces[1]));
time.setSeconds(pieces[2]);
console.log( time.setMinutes(pieces[1]));
var timedif = new Date(time.valueOf() - 1000);
var newTime=timedif.toTimeString().split(" ")[0];
document.getElementById('hms').innerHTML=newTime;
if(newTime==="00:00:00"){
clearTimeout(time_out);
$('#btn_next').css('display',' none');
alert("Sorry Your time is over");
}
time_out= setTimeout(count,1000);
}
count();
});
</script>