我制作了一个简单的测验脚本并在链接中使用$ _GET变量来确定需要询问哪个问题,当你提交它时,php header()以下一个问题的变量1开始。第一次,所以当变量为1(如http://www.test.com/quiz.php?q=1)时,答案检查效果很好,但当变量为2或更高时,他什么也没做,只是重定向到下一个问题......
任何人都知道为什么会这样?
编辑:我将php标头功能更改为html元标记,现在它完美运行:)
<!doctype html>
<html>
<head>
<script>
window.addEventListener('load', function () {
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var source = audioCtx.createBufferSource();
var xhr = new XMLHttpRequest();
xhr.open('GET', 'Soundtrack2.mp3');
xhr.responseType = 'arraybuffer';
xhr.addEventListener('load', function (r) {
audioCtx.decodeAudioData(
xhr.response,
function (buffer) {
source.buffer = buffer;
source.connect(audioCtx.destination);
source.loop = true;
});
source.start(0);
});
xhr.send();
});
</script>
<link rel="stylesheet" type="text/css" href="font.css">
<style>
.centersmall{
position: absolute;
right: 500px;
top: 50px;
width: 80px;
height: 80px;
}
.mogelijkheden{
position: absolute;
left: 200px;
top: 50px;
}
input[type=submit]{
font-family: WIDM, sans-serif;
width: 70px;
height: 25px;
border-radius: 10px;
}
</style>
</head>
<body>
<div class="centersmall">
<img src="/favicon.png">
</div>
<?php
session_start();
include('conn.php');
$sql1 = "SELECT TestResultaat FROM Spelers WHERE SpelerNaam='".$_SESSION['login_user']."'";
$query1 = mysqli_query($conn, $sql1);
while($result1 = mysqli_fetch_assoc($query1)){
$score_old = $result1['TestResultaat'];
$score = $score_old + 1;
}
echo $score;
$already = $_SESSION['alreadyanswered'];
$v = $_GET['v'];
if($already >= $v){
echo '<span style="color:red">Die heb je al beantwoord!</span>';
exit();
}
$sql = "SELECT * FROM Spelers WHERE SpelerNaam='".$_SESSION['login_session']."'";
$query = mysqli_query($conn, $sql);
while($result = mysqli_fetch_assoc($query)){
$ingevuld = $result['TestIngevuld'];
}
if($ingevuld == 1){
echo '<span style="color:red">Je hebt de test al gemaakt!</span>';
exit();
}
$sql = "SELECT * FROM VraagMogelijkheden WHERE VraagNummer=$v";
$query = mysqli_query($conn, $sql);
while($result = mysqli_fetch_assoc($query)){
$vraag1 = $result['Vraag'];
$answer1 = $result['Antwoord'];
}
$secondsql = "SELECT * FROM VraagMogelijkheden WHERE VraagNummer=$v";
$secondquery = mysqli_query($conn, $secondsql);
while($secondresult = mysqli_fetch_assoc($secondquery)){
$mogelijkheid1 = $secondresult['M1'];
$mogelijkheid2 = $secondresult['M2'];
$mogelijkheid3 = $secondresult['M3'];
$mogelijkheid4 = $secondresult['M4'];
$mogelijkheid5 = $secondresult['M5'];
$mogelijkheid6 = $secondresult['M6'];
$mogelijkheid7 = $secondresult['M7'];
$mogelijkheid8 = $secondresult['M8'];
$mogelijkheid9 = $secondresult['M9'];
$mogelijkheid10 = $secondresult['M10'];
$mogelijkheid11 = $secondresult['M11'];
$mogelijkheid12 = $secondresult['M12'];
$mogelijkheid13 = $secondresult['M13'];
echo '<div class="mogelijkheden">';
echo '<br>';
echo "<h3>$vraag1</h3>";
echo "<form name='vragen' method='post'>";
if(isset($mogelijkheid1)){
echo "<input type='radio' name='useranswer' value='1'>$mogelijkheid1";
}
echo '<br>';
if(isset($mogelijkheid2)){
echo "<input type='radio' name='useranswer' value='2'>$mogelijkheid2";
}
echo '<br>';
if(isset($mogelijkheid3)){
echo "<input type='radio' name='useranswer' value='3'>$mogelijkheid3";
}
echo '<br>';
if(isset($mogelijkheid4)){
echo "<input type='radio' name='useranswer' value='4'>$mogelijkheid4";
}
echo '<br>';
if(isset($mogelijkheid5)){
echo "<input type='radio' name='useranswer' value='5'>$mogelijkheid5";
}
echo '<br>';
if(isset($mogelijkheid6)){
echo "<input type='radio' name='useranswer' value='6'>$mogelijkheid6";
}
echo '<br>';
if(isset($mogelijkheid7)){
echo "<input type='radio' name='useranswer' value='7'>$mogelijkheid7";
}
echo '<br>';
if(isset($mogelijkheid8)){
echo "<input type='radio' name='useranswer' value='8'>$mogelijkheid8";
}
echo '<br>';
if(isset($mogelijkheid9)){
echo "<input type='radio' name='useranswer' value='9'>$mogelijkheid9";
}
echo '<br>';
if(isset($mogelijkheid10)){
echo "<input type='radio' name='useranswer' value='10'>$mogelijkheid10";
}
echo '<br>';
if(isset($mogelijkheid11)){
echo "<input type='radio' name='useranswer' value='11'>$mogelijkheid11";
}
echo '<br>';
if(isset($mogelijkheid12)){
echo "<input type='radio' name='useranswer' value='12'>$mogelijkheid12";
}
echo '<br>';
if(isset($mogelijkheid13)){
echo "<input type='radio' name='useranswer' value='13'>$mogelijkheid13";
}
echo '<br>';
echo '<input type="submit" name="submit" value="Ok">';
echo '</form>';
echo '</div>';
if(isset($_POST['submit'])){
$answer = $_POST['useranswer'];
if($answer == $answer1){
if($v != 20){
$n = $v;
$n += 1;
$_SESSION['alreadyanswered'] += 1;
header("Location: http://wieisdemol.rf.gd/test.php?v=$n");
$one = 0;
$sql = "UPDATE Spelers SET TestResultaat=?, TestIngevuld=? WHERE SpelerNaam=?";
$query = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($query, 'iis', $score, $one, $_SESSION['login_user']);
mysqli_stmt_execute($query);
}else{
$_SESSION['alreadyanswered'] += 1;
header("Location: http://www.wieisdemol.rf.gd/");
$one = 1;
$sql = "UPDATE Spelers SET TestResultaat=?, TestIngevuld=? WHERE SpelerNaam=?";
$query = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($query, 'iis', $score, $one, $_SESSION['login_user']);
mysqli_stmt_execute($query);
}
}else{
if($v != 20){
$n = $v;
$n += 1;
$one = 0;
$sql = "UPDATE Spelers SET TestResultaat=?, TestIngevuld=? WHERE SpelerNaam=?";
$query = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($query, 'iis', $score, $one, $_SESSION['login_user']);
mysqli_stmt_execute($query);
$_SESSION['alreadyanswered'] += 1;
header("Location: http://wieisdemol.rf.gd/test.php?v=$n");
}else{
$_SESSION['alreadyanswered'] += 1;
header("Location: http://www.wieisdemol.rf.gd/");
$one = 1;
$sql = "UPDATE Spelers SET TestResultaat=?, TestIngevuld=? WHERE SpelerNaam=?";
$query = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($query, 'iis', $score, $one, $_SESSION['login_user']);
mysqli_stmt_execute($query);
}
}
}
}
?>
答案 0 :(得分:0)
确保将session_start()
行移动到页面的最开头(即,在输出任何html或空格之前)。因为它发送标题!
另外,在回显任何html之前做你的逻辑事情。我的意思是验证用户提交的答案等。因为您使用header()
函数重定向到另一个页面,所以应该在将任何输出发送到浏览器之前使用它。即,在任何HTML代码或任何空格之前。
基本大纲如下:
<?php
//-- prefers it to be the first line, though you can move it down a couple of lines, but it should be placed before using the $_SESSION array
session_start();
//-- if user submitted any data..
if( isset( $_POST['submit'] ) )
{
//-- do the validation, answer verification, score updation, etc..
//...
if( $user_answer == $correct_answer )
{
//....
header( "Location: http://wieisdemol.rf.gd/test.php?v=" . $next_question_num );
}
//...
}
//-- do anything else you needed, say fetching the question details from the database for the current question, calculates the current score, etc..
//...
?><!doctype html>
<html>
<head></head>
<body>
............
</body>
</html>
关于您所面临的问题,请尝试调试您的代码。通过回显用户提交的数据,逐行检查数据等来说。您也可以使用var_dump()
来显示要检查的数据。
希望它有所帮助!