-2

This is my code, but I don't know where is the error.
It says that I got error which is Notice:

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
}

?>

I want to try to put isset. But I don't know how. I already try delete the include_once("config.php") to include("config.php").. but nothing happened.

I already start the session in the php. Can anyone help me to find what is wrong with my coding?

This coding is for view the profile of person who already login into the system. for example, I login into the system and when i view profile, only my profile will be display. therefore, I try to use the code that I post earlier.

But it is error. I already set my ic attribute in the database as primary key.

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
april
  • 1
  • 1

4 Answers4

1

Session variable is empty array on first request, so you could init it:

session_start(); 
if(!isset($_SESSION['ic']))
{
    $_SESSION['ic'] = 'my default value';
}
$ic = $_SESSION['ic'];
Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
PeterM
  • 1,478
  • 1
  • 22
  • 28
  • @Darly Gill Why would you edit a post that was wrong but up voted? PeterM didn't even add session_start() you did. Now; that's unfair – Poiz May 17 '16 at 08:19
  • @Poiz it's quite ok to improve someones answer. Anyway april states that he has already started session... – PeterM May 17 '16 at 09:46
1

Did you use a

session_start();

before you used any $_session in that PHP file?

Developer
  • 130
  • 13
0

You should make sure that Session already exists before making any attempts to access it. You started working with the Session without doing that. Here's my suggestion:

    <?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
        }

    ?>
Poiz
  • 7,611
  • 2
  • 15
  • 17
  • This will raise "undefined index" notice too. – PeterM May 17 '16 at 09:55
  • @PeterM **A NOTICE: YES!!!** ***NOT AN ERROR, NOT A FATAL ERROR, NOT EVEN A WARNING!!!*** But what is the value? **"NULL"** But let me edit it for ya, if it makes you happier.... – Poiz May 17 '16 at 09:58
  • @PeterM The code now contains a simple ternary operation construct: **isset($_SESSION['ic'])? $_SESSION['ic'] : null;** the difference being, it was still my edit... see the point? – Poiz May 17 '16 at 10:01
  • No need to be angry. This notice is a question problem. Proper php code should not raise notices, as this might lead to other errors. – PeterM May 17 '16 at 10:06
0

multiple declaration for $ic

if you have already created a session, then use:

session_start();

or if you want to create a new one, use:

$_SESSION['ic'] = $row['ic'];
Krish Munot
  • 1,093
  • 2
  • 18
  • 29
Akshay N Shaju
  • 355
  • 4
  • 17