获取有关php $ _session的错误

时间:2016-05-17 08:05:40

标签: php session

这是我的代码,但我不知道错误在哪里。
它说我收到的错误是通知:

Undefined index: ic in C:\xampp\htdocs\fyp\profile.php on line 57.

<?php
include_once("config.php");
$ic = $_SESSION['ic'];//-----------------------------------this is line 57
$sql = "SELECT * FROM studentprofile WHERE ic = '$ic'";
$result = mysql_query($sql,$conn);

while($row = mysql_fetch_array($result))
{
    $id      = $row['id'];
    $ic      = $row['ic'];
    $name    = $row['name'];
    $course  = $row['course'];
    $faculty = $row['faculty'];
    $address = $row['address'];
    $hpno    = $row['hpno'];
    $gender  = $row['gender'];
    $email  = $row['email'];
  ?>

<table width="70%" height="80%" border="1" align="center">
<tr>
<th>ID</th>
<td><?php echo $id;?></td>
</tr>
<tr>
<th>IC No</th>
<td><?php echo $ic;?></td>
</tr>
<tr>
<th>Name</th>
<td><?php echo $name;?></td>
</tr>
<tr>
<th>Course</th>
<td><?php echo $course;?></td>
</tr>
<tr>
<th>Faculty</th>
<td><?php echo $faculty;?></td>
</tr>
<tr>
<th>Address</th>
<td><?php echo $address;?></td>
</tr>
<tr>
<th>Handphone No</th>
<td><?php echo $hpno;?></td>
</tr>
<tr>
<th>Gender</th>
<td><?php echo $gender;?></td>
</tr>
<tr>
<th>Email</th>
<td><?php echo $email;?></td>
</tr>
</table>
<?php
}

?>

我想尝试放置isset。但我不知道怎么做。我已经尝试删除include_once(“config.php”)以包含(“config.php”)..但没有任何反应。

我已经在php中启动了会话。任何人都可以帮我找到我编码的错误吗?

此编码用于查看已登录系统的人员的个人资料。例如,我登录系统,当我查看个人资料时,只会显示我的个人资料。因此,我尝试使用我之前发布的代码。

但这是错误的。我已经将数据库中的ic属性设置为主键。

4 个答案:

答案 0 :(得分:1)

会话变量在第一次请求时是空数组,因此您可以初始化它:

session_start(); 
if(!isset($_SESSION['ic']))
{
    $_SESSION['ic'] = 'my default value';
}
$ic = $_SESSION['ic'];

答案 1 :(得分:1)

你使用过

吗?

在session_start();

在之前

你在那个PHP文件中使用了任何$ _session?

答案 2 :(得分:0)

在尝试访问Session之前,您应确保Session已存在。您开始使用Session而不这样做。这是我的建议:

    <?php
        // YOU SHOULD MAKE SURE THE SESSION EXIST FIRST BEFORE USING IT...
        // THIS SHOULD BE THE FIRST LINE OF CODE IN YOUR SCRIPT IF YOU ARE USING SESSION:           
        if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
            session_start();
        }

        include_once("config.php");
        $ic             = isset($_SESSION['ic'])? $_SESSION['ic'] : null;
        $sql            = "SELECT * FROM studentprofile WHERE ic = '$ic'";
        $result         = mysql_query($sql,$conn);

        while($row = mysql_fetch_array($result))
        {
            $id         = $row['id'];
            $ic         = $row['ic'];
            $name       = $row['name'];
            $course     = $row['course'];
            $faculty    = $row['faculty'];
            $address    = $row['address'];
            $hpno       = $row['hpno'];
            $gender     = $row['gender'];
            $email      = $row['email'];
            ?>

            <table width="70%" height="80%" border="1" align="center">
                <tr>
                    <th>ID</th>
                    <td><?php echo $id;?></td>
                </tr>
                <tr>
                    <th>IC No</th>
                    <td><?php echo $ic;?></td>
                </tr>
                <tr>
                    <th>Name</th>
                    <td><?php echo $name;?></td>
                </tr>
                <tr>
                    <th>Course</th>
                    <td><?php echo $course;?></td>
                </tr>
                <tr>
                    <th>Faculty</th>
                    <td><?php echo $faculty;?></td>
                </tr>
                <tr>
                    <th>Address</th>
                    <td><?php echo $address;?></td>
                </tr>
                <tr>
                    <th>Handphone No</th>
                    <td><?php echo $hpno;?></td>
                </tr>
                <tr>
                    <th>Gender</th>
                    <td><?php echo $gender;?></td>
                </tr>
                <tr>
                    <th>Email</th>
                    <td><?php echo $email;?></td>
                </tr>
            </table>
            <?php
        }

    ?>

答案 3 :(得分:0)

$ ic

的多重声明

如果您已经创建了会话,请使用:

session_start();

或者如果您想创建一个新的,请使用:

$_SESSION['ic'] = $row['ic'];