-2

I got an error:

Notice: Undefined variable: pollid

What's wrong here?

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

?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Infed
  • 87
  • 1
  • 2
  • 5

2 Answers2

0

You need to see where the value of $_GET['pollid'] is coming from.

Probably the error is coming because the variable $pollid is not getting the value. As a result of this it is unset and an error message is getting displayed. Please check the form from where you are making an HTTP GET request. Once the value is received, the problem will be solved.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

$_GET['pollid'] is not declared.

There are chances that $_GET and $_POST variables are not declared in some situation. Using isset() helps you to check if the variable is declared or not.

For example,

if(isset($_GET['pollid']))
{
   // Your code here
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
FrozenFire
  • 671
  • 14
  • 28