PHP会话为空或无法正常工作

时间:2014-04-29 16:20:06

标签: php mysql sql session-variables

我使用会话变量$_SESSION['staff_id']作为查询字符串中条件的一部分。只是为了查看变量是否正常工作,我将其设置为echo,作为变量$wombat,如代码中所示,但它是空的。我不知道我做错了什么。

为了确保我的语法正确并且据我所知,我已经遍布这个地方。

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

1 个答案:

答案 0 :(得分:1)

您的作业无效,(我猜)您忘记将第一个if换成大括号。

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 */
}