【发布时间】:2020-06-06 09:54:20
【问题描述】:
我快要创建一个基本的测验应用程序了。作为一个新程序员,我知道我会犯很多错误,但我想从中学习。 所以我遇到了一些小问题,最近的一个问题是我的分数 变量设置不正确。
我主要是因为测验结束时的结果得分为零。
我不知道错误是什么。这就是为什么我会非常感谢任何帮助或建议。真的非常感谢。
这是我的代码的基本运行:
问题.php
<?php include "database.php";
?>
<?php session_start(); ?>
<?php
//Set question
$number = (int) $_GET['n'];
/*
* Get Total Questions
*/
$query="SELECT * FROM questions" ;
//Get Results
$results = mysqli_query ($conn,$query);
$total = mysqli_num_rows($results);
/*
* Get the Question
*/
$query = "SELECT * FROM questions
WHERE question_number = $number";
//Get result
$result = mysqli_query ($conn,$query);
$question = $result->fetch_assoc();
/*
* Get choices
*/
$query = "SELECT * FROM choices
WHERE question_number = $number";
//Get result
$choices = mysqli_query($conn,$query);
?>
<?php echo $question ['text']; ?>
</p>
<form method="post" action="process.php"
<ul class="choices">
<?php while ($row = $choices->fetch_assoc()) : ?>
<li><input name="choice" type="radio" value="<?php echo $row ['id'];?> " /><?php echo $row ['text']; ?></li>
<input type="submit" name="submit" value="Sumbit"/>
<input type="hidden" name= "number" value="<?php echo $number; ?>" />
</main>
</body>
</html>
<?php endwhile; ?>
进程.php
<?php include "database.php";
?>
<?php session_start(); ?>
<?php
// check to see if score is set error_handler)
if(!isset($_SESSION['score'] )) {
$_SESSION ['score'] = 0;
}
if($_POST) {
$number = $_POST['number'];
$selected_choice = $_POST['choice'];
$next = $number+1;
/*
* Get Total Questions
*/
$query="SELECT * FROM questions" ;
//Get Results
$results = mysqli_query ($conn,$query);
$total = mysqli_num_rows($results);
/*
* Get correct choice
*/
$query = "SELECT * FROM choices
WHERE question_number = $number AND is_correct=1";
//Get result
$result = mysqli_query($conn,$query);
//Get Row
$row = $result->fetch_assoc();
//Set Correct choice
$correct_choice = $row['id'];
//Compare choice
if ($correct_choice = $selected_choice) {
//The answer is correct
$_SESSION['score']+1 ;
}
//Check if it is the last question
if($number == $total){
header("Location: final.php") ;
exit();
}
else {
header("Location: question.php?n=".$next);
}
<p>Final score: <?php echo $_SESSION ['score']; ?> <p>
【问题讨论】:
-
会话开始应该在您的页面顶部,然后是其他任何内容。然后包含您的文件
-
非常感谢您的帮助。然而不幸的是它仍然无法正常工作
-
?> <?php可以删除;您正在关闭 PHP,然后是空格,然后重新打开它 -
很公平,谢谢你,我会这样做的。但不幸的是,看起来这个分数问题需要更长的时间才能解决。