سؤال

I have following code:

<?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']++;

?>

What this code does is showing questions from database which have kysimustik_id="' . $_SESSION['pollid']. Everything works the way it has to (There are two pieces of code in adition, one for showing and answering text questions and one for radio button questions.)

My problem is that I want it to echo out that Survey is over, when all questions have answer, but I dont know how I should rearrange my code to do it. Right now I get these errors, when all questions are answered:

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

I tried to change code to that:

<?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';
?>

But I still get:

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

I don't know how I should rearrange things to lose this notice, without messing anything up.

And second problem that appears after this change is that the last answer of survey wont be inserted into database. I understand why it happens, but I don't know how to solve this.

هل كانت مفيدة؟

المحلول

change (line 2 and 3)

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

into

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

Also this script allows sql injection and (probably) also XSS (security!) and uses a deprecated api to mysql.

نصائح أخرى

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

Instead of

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

Use:

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

http://www.php.net/isset

http://www.php.net/empty

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top