如何从PHP调查结束时删除通知和警告?

时间:2014-04-25 11:33:45

标签: php html mysql undefined

我有以下代码:

<?php

include_once 'init/init.funcs.php';
$_SESSION['pollid'] = (int) $_GET['pollid'];
$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']的问题。一切都按照它的方式工作(adition中有两段代码,一段用于显示和回答文本问题,另一条用于单选按钮问题。)

我的问题是,我想让它回应一下调查已经结束,当所有问题都有答案时,但我不知道如何重新安排我的代码来完成它。现在,当所有问题都得到解答时,我会收到这些错误:

Notice: Undefined index: answer in C:\xampp2\htdocs\Praks\answering.php on line 5

Notice: Undefined offset: 7 in C:\xampp2\htdocs\Praks\answering.php on line 16

Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 7 in C:\xampp2\htdocs\Praks\answering.php on line 17

我尝试将代码更改为:

<?php
include_once 'init/init.funcs.php';
$_SESSION['pollid']=(int) $_GET['pollid'];
$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']) 
{
    $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']++;
}

else
    echo 'Küsitlus lõppenud';
?>

但我仍然得到:

Notice: Undefined index: pollid in C:\xampp2\htdocs\Praks\answering.php on line 3

我不知道如何重新安排事情以免丢失此通知,而不会搞砸任何事情。

此次更改后出现的第二个问题是调查的最后一个答案不会插入到数据库中。我明白为什么会这样,但我不知道如何解决这个问题。

3 个答案:

答案 0 :(得分:2)

改变(第2和第3行)

include_once 'init/init.funcs.php';
$_SESSION['pollid']=(int) $_GET['pollid'];

include_once 'init/init.funcs.php';
if (isset($_GET['pollid']))
    $_SESSION['pollid']=(int) $_GET['pollid'];
else
    $_SESSION['pollid'] = 0;

此脚本也允许sql注入和(可能)XSS(安全!)并使用不推荐使用的api到mysql。

答案 1 :(得分:2)

if (isset($_GET['pollid']))
    $_SESSION['pollid']=(int) $_GET['pollid'];
else
    $_SESSION['pollid'] = 0;

答案 2 :(得分:0)

而不是

if (!isset($_SESSION['answering'])) {

使用:

if (empty($_SESSION['answering'])) {

http://www.php.net/isset

http://www.php.net/empty