-3
   <?php
    include "connection.php";

     if(!isset($_SESSION)) 
        { 
            session_start(); 
        } 
    $cand1  = $_POST['cand1'];

    $cand2  = $_POST['vice1'];





    $sess   =  $_SESSION['SESS_NAME'];




    if(!$cand1){
    $error="<center><h4><font color='#FF0000'>Please fill empty fields</h4></center></font>";
    include"student.php";
    exit();
    }



        $cand1 = addslashes($cand1);


        $cand1 = mysqli_real_escape_string($con,$cand1);







    $sql = 'SELECT * FROM student WHERE username="'.$_SESSION['SESS_NAME'].'" AND status="VOTED"';





    $result = mysqli_query($con,$sql);

                if (mysqli_num_rows($result)==1){

            $msg="<center><h4><font color='#FF0000'>You have already been voted, No need to vote again</h4></center></font>";
            include 'student.php';
            exit(); 
                }

                else{

                 $sql = 'UPDATE candidate SET votecount = votecount + 1 WHERE cand_id = "'.$_POST['cand1'].'" OR cand_id = "'.$_POST['vice1'].'"';






                $sql2 = 'UPDATE student SET status="VOTED" WHERE username="'.$_SESSION['SESS_NAME'].'"';


                $result = mysqli_query($con,$sql);
                $result2 = mysqli_query($con,$sql2);

        if(!$result && !$result2){
        die("Error on mysql query".mysqli_error());
        }
        else{
        $msg="<center><h4><font color='#FF0000'>Congratulation, you have made your vote.</h4></center></font>";
        include 'student.php';
        exit();
        }
                }


    ?>

the errors are at code including $sess= $_SESSION['SESS_NAME']; and at username="'.$_SESSION['SESS_NAME'].'" i have tried every possibility so can you check my code? mainly the error is undefined index at $session[session_name]?

  • You are trying to access a variable in `$_SESSION` that doesn't exist. Do you set the `$_SESSION['SESSION_NAME']` in the code that leads to `student_vote.php`? You can read about how to set the session in the [docs](http://php.net/manual/en/function.session-name.php) –  Jun 07 '17 at 13:52
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – LF00 Jun 07 '17 at 13:53
  • Yeah it possibly is – Randy Jun 07 '17 at 13:56

2 Answers2

0

Are you sure the session 'SESS_NAME' is getting set? In the code above you are only trying to access its value. Undefined index is the error thrown when there is no session with that name.

Randy
  • 543
  • 1
  • 5
  • 16
0

Looks like to me you haven't declared the variable $_SESSION['SESS_NAME']. Try doing the following

var_dump($_SESSION);

This will show you all the defined variables in the session, if SESS_NAME isn't there then it hasn't been defined. Maybe you have failed to define it somewhere else?