获取有关php$_session的此错误


Getting this error regarding 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
}
?>

我想试着把它放进去。但我不知道怎么做。我已经尝试删除include_one("config.php")以包含("configphp"),但什么也没发生。

我已经在php中开始了会话。有人能帮我找出我的编码出了什么问题吗?

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

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

您使用了吗

session_start();

之前您在该PHP文件中使用了任何$_session?

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

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

在尝试访问会话之前,您应该确保会话已经存在。您开始使用会话时没有这样做。这是我的建议:

    <?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
        }
    ?>
$ic 的多重声明

如果你已经创建了一个会话,那么使用:

session_start();

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

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