如何在回答结束时删除调查变量?

时间:2014-04-25 12:25:44

标签: php html

我有以下代码:

<?php
ini_set("error_reporting",0);
include_once 'init/init.funcs.php';
if (isset($_GET['pollid']))
    $_SESSION['pollid']=(int) $_GET['pollid'];
else
    $_SESSION['pollid'] = 0;
    echo 'kysitlus l6ppenud';
$questions = array();
if (!isset($_SESSION['answering'])) {
    $result = mysql_query('SELECT * from katse_kysimused where kysimustik_id="' . $_SESSION['pollid'] . '"');
    while($row = mysql_fetch_assoc($result)) {
        $questions[] = $row['kysimus'];
        }
    $_SESSION['answering']['questions'] = $questions;
    $_SESSION['answering']['index'] = 0;
    $_SESSION['answering']['count']=count($questions);
}

    $answer = $_POST['answer'];
    $x = $_SESSION['answering']['index'];
    $result3 = mysql_query('SELECT tyyp_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x] . '"');
    $type = mysql_result($result3, 0);
    if ($type=='3'){
        echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/valikvastustega_kysimusele_vastamine.php'>";
        }

    if ($type=='1'){
            echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/tekstkysimusele_vastamine2.php'>";
        }

if(isset($_POST['submit'])){
    if(isset($_POST['option'])){
    $answer=$_POST['option'];
    }
    $result2 = mysql_query('SELECT kysimus_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x -1] . '"');
    $q_id = mysql_result($result2, 0);
    mysql_query('INSERT INTO katse_vastused2 (id, vastus,kysimus_id, vastustik_id) VALUES (NULL,"' . $answer . '","' . $q_id . '","1")');
}
$_SESSION['answering']['index']++;

?>

这是从kysimustik_id="' . $_SESSION['pollid']的数据库打印出答案的代码。(这不是完整的代码,有两个单独的文件用于回应和回答问题)。

我的问题是,我应该在我的代码中写什么和哪里,所以$ _SESSION [&#39;回答&#39;]变量将在回答结束时被删除,所以我可以在回答期间再次回答其他调查没有关闭并重新打开浏览器的同一会话。

编辑:

我改变了我的代码,现在我可以多次回答调查,但它始终是我回答的第一个调查,我从url插入哪个pollid并不重要,我的代码现在看起来像这样:< / p>

<?php
ini_set("error_reporting",0);
include_once 'init/init.funcs.php';
if (isset($_GET['pollid']))
    $_SESSION['pollid']=(int) $_GET['pollid'];
else
    $_SESSION['pollid'] = 0;
    //echo 'kysitlus l6ppenud';
$questions = array();
if (!isset($_SESSION['answering'])) {
    $result = mysql_query('SELECT * from katse_kysimused where kysimustik_id="' . $_SESSION['pollid'] . '"');
    while($row = mysql_fetch_assoc($result)) {
        $questions[] = $row['kysimus'];
        }
    $_SESSION['answering']['questions'] = $questions;
    $_SESSION['answering']['index'] = 0;
    $_SESSION['answering']['count']=count($questions);
}

if ($_SESSION['answering']['index']==$_SESSION['answering']['count']){
if(isset($_SESSION['answering'])){
  unset($_SESSION['answering']);
  unset ($_SESSION['answering']['questions']);
  unset ($_SESSION['answering']['index']);
  unset ($_SESSION['answering']['count']);
  exit("kysitlus l6ppenud");
  }
}

    $answer = $_POST['answer'];
    $x = $_SESSION['answering']['index'];
    $result3 = mysql_query('SELECT tyyp_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x] . '"');
    $type = mysql_result($result3, 0);
    if ($type=='3'){
        echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/valikvastustega_kysimusele_vastamine.php'>";
        }

    if ($type=='1'){
            echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/tekstkysimusele_vastamine2.php'>";
        }

if(isset($_POST['submit'])){
    if(isset($_POST['option'])){
    $answer=$_POST['option'];
    }
    $result2 = mysql_query('SELECT kysimus_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x -1] . '"');
    $q_id = mysql_result($result2, 0);
    mysql_query('INSERT INTO katse_vastused2 (id, vastus,kysimus_id, vastustik_id) VALUES (NULL,"' . $answer . '","' . $q_id . '","1")');
}
$_SESSION['answering']['index']++;


?>

然后我将($_SESSION['answering']['index']==$_SESSION['answering']['count'])更改为($_SESSION['answering']['index']==$_SESSION['answering']['count']+1),现在我可以回答任何调查,但会出现新问题。当调查结束时,kysitlus l6ppenud&#39;不会马上出现。我必须重新加载页面,然后出现消息。我确信问题有简单的解决方案,但我无法弄明白,我应该改变一些代码或其他东西的顺序。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

通过这种方式解决问题:

<?php
ini_set("error_reporting",0);
include_once 'init/init.funcs.php';
if (isset($_GET['pollid']))
    $_SESSION['pollid']=(int) $_GET['pollid'];
else
    $_SESSION['pollid'] = 0;
$questions = array();
if (!isset($_SESSION['answering'])) {
    $result = mysql_query('SELECT * from katse_kysimused where kysimustik_id="' . $_SESSION['pollid'] . '"');
    while($row = mysql_fetch_assoc($result)) {
        $questions[] = $row['kysimus'];
        }
    $_SESSION['answering']['questions'] = $questions;
    $_SESSION['answering']['index'] = 0;
    $_SESSION['answering']['count']=count($questions);
}


    $answer = $_POST['answer'];
    $x = $_SESSION['answering']['index'];
    $result3 = mysql_query('SELECT tyyp_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x] . '"');
    $type = mysql_result($result3, 0);
    if ($type=='3'){
        echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/valikvastustega_kysimusele_vastamine.php'>";
        }

    if ($type=='1'){
            echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/tekstkysimusele_vastamine2.php'>";
        }

if(isset($_POST['submit'])){
    if(isset($_POST['option'])){
    $answer=$_POST['option'];
    }
    $result2 = mysql_query('SELECT kysimus_id FROM katse_kysimused where kysimus= "' . $_SESSION['answering']['questions'][$x -1] . '"');
    $q_id = mysql_result($result2, 0);
    mysql_query('INSERT INTO katse_vastused2 (id, vastus,kysimus_id, vastustik_id) VALUES (NULL,"' . $answer . '","' . $q_id . '","1")');
    if ($_SESSION['answering']['index']==$_SESSION['answering']['count']){
if(isset($_SESSION['answering'])){
  unset($_SESSION['answering']);
  unset ($_SESSION['answering']['questions']);
  unset ($_SESSION['answering']['index']);
  unset ($_SESSION['answering']['count']);
  exit("kysitlus l6ppenud");
  }
}
}
$_SESSION['answering']['index']++;


?>