简体   繁体   English

PHP脚本中的变量不增加

[英]Variable does not increment in PHP script

First: This is not a repeat of this question . 第一:这不是这个问题的重复。 Same script, but different question. 相同的脚本,但问题不同。

I had initially thought the error in my previous question would have solved this error, but it didn't. 我最初以为上一个问题中的错误可以解决此错误,但事实并非如此。

I'm making a simple 3-question quiz via PHP. 我正在通过PHP进行一个简单的3题测验。 The quiz runs without syntax errors, however the problem is that my variable $correct does not seem to increment throughout the quiz (given a correct answer). 测验没有语法错误,但是问题是我的变量$ correct在整个测验中似乎没有增加(给出正确答案)。 I put print statements to see if it changes but nothing gets printed. 我放入打印语句以查看它是否更改,但是什么都没有打印出来。 Somehow its not properly incrementing the value of the variable. 不知何故,它不能正确地增加变量的值。

Instead of outputting: 而不是输出:

 You got 2 out of 3 correct. //where 2 is the value of $correct

It outputs: 它输出:

You got correct out of correct. //where the value $correct seems to have no value

What am I doing wrong? 我究竟做错了什么?

 <?php
 ini_set('session.gc_maxlifetime',900);
//echo ini_get("session.gc_maxlifetime"); 
session_start();
if($_SESSION['loggedin'] !== 1) {
header('Location: login.php');
exit;
}

 if(isset($_SESSION["blah"]))
{
$_SESSION["number"] = 0;
$_SESSION["correct"] = 0;
}

$total_number = 3;

print <<<TOP
<html>
<head>
<title> History Quiz </title>
</head>
<body>
<h3> History Quiz </h3>
TOP;

$number = $_SESSION["number"];
$correct = $_SESSION["correct"];


if ($number == 0){
print <<<FIRST
<p> You will be given $total_number questions in this quiz. <br /><br/>
  You will have 15 minutes to complete it. <br /><br/>
  You cannot go back to change previous answers.<br /><br/>
  Here is your first question: <br /><br />
</p>
<p>1. Abe Lincoln was born in Illinois.</p>
<p>
    <label><input type="radio" name="question1" value="true" /> True </label>
    <label><input type="radio" name="question1" value="false" /> False </label>
</p>
FIRST;

if (isset($_POST['submit'])) {

$selected_radio = $_POST['question1'];

if ($selected_radio == 'false') {
   $correct++;
print $correct;
   $_SESSION["correct"] = $correct;

}
}
}


if ($number == 1){
print <<<SECOND
<p>2. In what state was the battle of Gettysburg fought?</p>
<p>
    <label><input type="checkbox" name="question2" value="Texas" /> a) Texas  
</label><br/>
    <label><input type="checkbox" name="question2" value="Pennsylvania" /> b)  
Pennsylvania </label><br/>
    <label><input type="checkbox" name="question2" value="Virginia" /> c) Virginia   
</label><br/>
    <label><input type="checkbox" name="question2" value="West Virginia" /> d) West   
Virginia </label>
</p>
SECOND;

if (isset($_POST['submit'])) {

if(isset($_POST['question2']) &&
$_POST['question'] == 'Pennsylvania')
{
   $correct++;
   $_SESSION["correct"] = $correct;  
}

}
}


if ($number == 2){
print <<<THIRD
<p>3. The last name of the commander of the Army of North Virginia was __________.</p>
<p>
    <input type='text' id='question3' />
THIRD;

if (isset($_POST['submit'])) {

$selected_answer = $_POST['question3'];

if ($selected_answer == "lee") {
   $correct++;
   $_SESSION["correct"] = $correct;
 }
 }
 }


if ($number >= $total_number)
{
print <<<FINAL_SCORE
Your final score is $correct correct out of $total_number. <br /><br />
Thank you for playing. <br /><br />
FINAL_SCORE;
session_destroy();
}
else
{
$number++;
$_SESSION["number"] = $number;
$script = $_SERVER['PHP_SELF'];
print <<<FORM
<form method = "post" action = $script>
<input type = "submit" value = "Check Answer" />
</form>
FORM;
}

?>

Julio, my current code (2nd Update): Julio,我当前的代码(第二次更新):

<?php
ini_set('session.gc_maxlifetime',900);
session_start();
if($_SESSION['loggedin'] !== 1) {
header('Location: login.php');
exit;
}

if(isset($_SESSION["correct"])){
$correct = $_SESSION["correct"];
} else {
$number = 0;
$correct = 0;
}

// check if which question was submitted
if (isset($_POST['submit'])) {
// set $number = the question that was submitted
$number = $_POST['question'];
switch ($_POST['question']){
    case 1:
        if ($_POST['answer']) $correct++; // this answer should be 'true'
        break;
    case 2:
        if ($_POST['answer'] == 2) $correct++; // this answer should be 'PA'
        break;
    case 3:
    if ($_POST['answer'] == "lee") $correct++; //this answer should be 'lee'
}
}   

// set the session correct var to our current tally
$_SESSION['correct'] = $correct;



$total_number = 3;

print <<<TOP
<html>
<head>
<title> History Quiz </title>
</head>
<body>
<h3> History Quiz </h3>
TOP;

if ($number == 0){
print <<<FIRST
<p> You will be given $total_number questions in this quiz. <br /><br/>
  You will have 15 minutes to complete it. <br /><br/>
  You cannot go back to change previous answers.<br /><br/>
  Here is your first question: <br /><br />
</p>
<p>1. Abe Lincoln was born in Illinois.</p>
<p>
    <label><input type="radio" name="answer" value="true" /> True </label>
    <label><input type="radio" name="answer" value="false" /> False </label>
    <input type="hidden" name="question" value="1" />
</p>
FIRST;
}


if ($number == 1){
print <<<SECOND
<p>2. In what state was the battle of Gettysburg fought?</p>
<p>
<label><input type="checkbox" name="answer" value="1" /> a) Texas  
</label><br/>
<label><input type="checkbox" name="answer" value="2" /> b)  
Pennsylvania </label><br/>
<label><input type="checkbox" name="answer" value="3" /> c) Virginia   
</label><br/>
<label><input type="checkbox" name="answer" value="4" /> d) West   
Virginia </label>
<input type="hidden" name="question" value="2" />
</p>
SECOND;
}
if ($number == 2){
print <<<THIRD
 <p>3. The last name of the commander of the Army of North Virginia was __________.</p>
 <p>
    <input type='text' id='answer' />
<input type="hidden" name="question" value="3" />
 </p>
THIRD;
}

if ($number >= $total_number)
{
print <<<FINAL_SCORE
Your final score is $correct correct out of $total_number. <br /><br />
Thank you for playing. <br /><br />
FINAL_SCORE;
session_destroy();
}
else {
 $number++;
 $_SESSION['number'] = $number;
 $script = $_SERVER['PHP_SELF'];
 print <<<FORM
<form method = "post" action = $script>
<input type = "submit" value = "Check Answer" />
</form>
FORM;
}


?>

Your first few lines at the top... 您顶部的前几行...

if(isset($_SESSION["blah"]))
{
  $_SESSION["number"] = 0;
  $_SESSION["correct"] = 0;
}

this means that $_SESSION['correct'] will only have a value if the $_SESSION['blah'] variable is set to begin with. 这意味着,如果将$ _SESSION ['blah']变量设置为开头,则$ _SESSION ['correct']仅具有一个值。 This could be your issue because if $_SESSION['correct'] doesn't have a value, none of the increments and assignment into $session won't work further down. 这可能是您的问题,因为如果$ _SESSION ['correct']没有值,则$ session的增量和赋值都不会进一步降低。

From a quick read of the code it looks like you're refreshing the page by posting to itself-- that means you're resetting the $correct variable to 0 each time. 通过快速阅读代码,您似乎可以通过发布到页面本身来刷新页面-这意味着您每次都将$correct变量重置为0。 If you want to increment $correct , increment the value in the SESSION, then read out the session $correct value into the page's $correct var so it isn't reset on each form submit. 如果你想增加$correct ,递增的SESSION的值,然后读出会话$correct值到页面的$correct VAR所以它不是每个表单提交重置。

EDIT TO ADD SOME SAMPLE CODE: 编辑以添加一些示例代码:

<?php

session_start();
if($_SESSION['loggedin'] !== 1) {
header('Location: login.php');
exit;
}

if(isset($_SESSION["correct"])){
    $correct    = $_SESSION["correct"];
} else {
    $number     = 0;
    $correct    = 0;
}

// check if which question was submitted
if (isset($_POST['submit'])) {
    // set $number = the question that was submitted
    $number = $_POST['question'];
    switch ($_POST['question']){ //<-----replace this line
        case 1:
            if ($_POST['answer']) $correct++; // this answer should be 'true'
            break;
        case 2:
            if ($_POST['answer'] == 2) $correct++; // this answer should be 'PA'
            break;
        //etc.
    }
}   

// set the session correct var to our current tally
$_SESSION['correct'] = $correct;



$total_number = 3;

print <<<TOP
<html>
<head>
<title> History Quiz </title>
</head>
<body>
<h3> History Quiz </h3>
TOP;

if ($number == 0){
    print <<<FIRST
    <p> You will be given $total_number questions in this quiz. <br /><br/>
      You will have 15 minutes to complete it. <br /><br/>
      You cannot go back to change previous answers.<br /><br/>
      Here is your first question: <br /><br />
    </p>
    <p>1. Abe Lincoln was born in Illinois.</p>
    <p>
        <label><input type="radio" name="answer" value="true" /> True </label>
        <label><input type="radio" name="answer" value="false" /> False </label>
        <input type="hidden" name="question" value="1" />
    </p>
    FIRST;
}


if ($number == 1){
print <<<SECOND
<p>2. In what state was the battle of Gettysburg fought?</p>
<p>
    <label><input type="checkbox" name="answer" value="1" /> a) Texas  
</label><br/>
    <label><input type="checkbox" name="answer" value="2" /> b)  
Pennsylvania </label><br/>
    <label><input type="checkbox" name="answer" value="3" /> c) Virginia   
</label><br/>
    <label><input type="checkbox" name="answer" value="4" /> d) West   
Virginia </label>
<input type="hidden" name="question" value="2" />
</p>
SECOND;
etc. . .

I would take out the 我会拿出

if(isset($_SESSION["blah"]))
{
  $_SESSION["number"] = 0;
  $_SESSION["correct"] = 0;
}

altogether as this would mean it is reset on page refresh. 完全意味着这将在页面刷新时重置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM