PHP中未定义的变量错误。怎么修?

时间:2016-06-14 08:02:51

标签: php error-handling compiler-errors syntax-error runtime-error

  

我收到错误:注意:未定义的变量:pollid这里有什么问题?

<?php

        $pollid = $_GET['pollid'];
        $connect = mysqli_connect('localhost', 'root','test','apklausa1');
        $query = "SELECT * FROM polls WHERE pollid='$pollid'";
        $q = mysqli_query($connect, $query);
        while($row = mysqli_fetch_array($q)) {
            $id = $row[0];
            $title = $row[1];
            $pollid = $row[2];
            $ipaddress = $row[3];
            echo "<h1>$title</h1>";
            ?>
            <table>
                <form action="" method="POST">
            <?php
                $questions = "SELECT * FROM questions WHERE pollid='$pollid'";
                $q2 = mysqli_query($connect, $questions);
                while($r = mysqli_fetch_array($q2)) {
                $question = $r[1];
                $votes = $r[2];
                $newvotes = $votes + 1;
                $ip = $_SERVER['REMOTE_ADDR'];
                $newipaddress = $ipaddress."$ip,";

                if (isset($_POST['vote'])) {
                    $polloption = $_POST['polloption'];
                    if ($polloption == "") {
                        die("You didn't select an option.");
                    } else {

                            $ipaddresse = explode(",", $ipaddress);
                            if (in_array($ip, $ipaddresse)) {
                                die("You've already voted");
                            } else {
                        mysqli_query($connect, "UPDATE questions SET votes='$newvotes' WHERE pollid='$pollid' AND question='$polloption'");
                        mysqli_query($connect, "UPDATE polls SET ipaddress='$newipaddress' WHERE pollid='$pollid'");
                        die("You voted Successfully");
                        }
                    }
                }

                echo '<tr><td>'.$question.'</td><td><input type="radio" name="polloption" value="'.$question.'" /> '.$votes.' votes</td></tr>';
                }
        }
        ?>

2 个答案:

答案 0 :(得分:0)

您需要查看$ _GET ['pollid']值的来源。可能错误来了,因为变量$ pollid没有获得该值。因此,它未设置并且显示错误消息。请检查您发出HTTP GET请求的表单。一旦收到价值,问题就会得到解决。

答案 1 :(得分:0)

$_GET['pollid']未声明。

在某些情况下,有可能未声明$_GET$_POST个变量。使用isset()可以帮助您检查是否声明了变量。

e.g。

if(isset($_GET['pollid']))
{
   // YOUR CODES HERE
}