user3487244
user3487244

Reputation: 147

PHP session is either empty or doesn't work correctly

I am using the session variable $_SESSION['staff_id'] to use as part of a condition in a query string. Just to see if the variable is working I set it to echo as the variable $wombat as seen in the code but instead it is empty. I don't know what I am doing incorrectly.

I have looked all over the place just to make sure my syntax is correct and to my knowledge it is.

<?php 
SESSION_START();
define('INCLUDE_CHECK',true);
if(isset($_SESSION['staff_id']))
$_SESSION['staff_id']=$wombat;
echo "'$wombat'";

// get the overall page header, including bootstrap and datatables libraries
// as well as the navbar menu for the top of the web page
require_once('task_assignment_header.php');

// load DB name and user credentials from single file for entire website
// this makes life easier later on
require_once('db_credentials.php');

?>
    <div class="container" style='margin-top:50px;'>

      <?php

        $StaffDBconnection = new mysqli($db_hostname, $db_username, $db_userpass, $db_dbname);

        // Check connection
        if (mysqli_connect_errno())
        {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        exit; // end program execution here
        }

        $resultset = mysqli_query($StaffDBconnection,"select t1.project_id, t1.project_name, t1.activeproject, t2.task_id, t2.taskname,
                                                      t2.taskstatus, t2.project_id, t3.staff_id, t3.fName, t3.lName, t4.staff_id, t4.task_id
                                                      from projects t1, tasks t2, employee t3, emp_tasks t4
                                                      where t1.activeproject = 'open'
                                                      and t2.taskstatus = 'in progress'
                                                      and t1.project_id = t2.project_id 
                                                      and t2.task_id = t4.task_id
                                                      and t3.staff_id = t4.staff_id
                                                      and t3.staff_id=" .$_SESSION['staff_id']);










        echo "<br><br>"; // put in a couple of blank lines at top below menu

        // This div tag makes the table "responsive" on smaller screens
        // and it also controls the overall width of the table.
        // Note that its width class of col-xs-5 is the sum of the width
        // classes used in the column heading (th) tags within the table, itself.
        echo "<div class='table-responsive col-xs-6'>";
                        // main table of data
            echo "<table id='assigntable' cellpadding='0' cellspacing='0' border='0' class='table table-striped table-bordered'>";
            echo "<thead><tr><th class='col-lg-4'>Project Name</th><th class='col-sm-4'>Task Name</th><th class='col-sm-4'>Employee Name</th></tr></thead><tbody>";

            while($AssignRow = mysqli_fetch_array($resultset))
              {
              $tmpID = $AssignRow['task_id'];
              $tmpprojid = $AssignRow['project_id'];


              echo "<tr><td>" . $AssignRow['project_name'] . "</td>" 
                     . "<td>" . $AssignRow['taskname'] . "</td>" . "<td>" . $AssignRow['fName']." ". $AssignRow['lName']. "</td>" . "</td>" ."</tr>";
               }

            echo "</tbody></table>";
            // end of main table of data

        echo "</div>"; // close out the responsive table div

        mysqli_close($StaffDBconnection);

      ?>



    </div><!-- /.container -->

<?php  
require_once('bootstrap_stafftable_footer.php');
?>

Upvotes: 1

Views: 75

Answers (1)

potashin
potashin

Reputation: 44581

Your assignment is invalid, also (I guess) you've forgotten to wrap your first if in braces.

session_start();
define('INCLUDE_CHECK',true);
if(isset($_SESSION['staff_id'])){
    $wombat = $_SESSION['staff_id'];
   echo "$wombat";
} else {
   echo "Undefined index 'staff_id'"; /*To check if $_SESSION['staff_id'] is not defined */
}

Upvotes: 1

Related Questions