2016-05-17 47 views
-2

這是我的代碼,但我不知道錯誤在哪裏。
它說,我有錯誤,是通知:

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屬性設置爲主鍵。

+0

當您設置$ _SESSION ['ic']的值? –

+0

你開始會話嗎?否則檢查'$ ic = isset($ _SESSION ['ic'])? $ _SESSION ['ic']:'';' –

+0

yes sir.i已經開始會話。 – april

回答

1

Session變量是在第一次請求空數組,所以你可以初始化它:

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

@Darly Gill爲什麼你會編輯一個錯誤的帖子,但是投了票? PeterM甚至沒有添加你所做的session_start()。現在;這是不公平 – Poiz

+0

@Poiz提高某些人的答案是相當好的。無論如何,4月份,他已經開始會議... – PeterM

1

是否使用了

在session_start();

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

+0

是的先生。已經開始會話 – april

0

在進行任何嘗試訪問之前,您應該確保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 
     } 

    ?> 
+0

這也會引發「未定義索引」的通知。 – PeterM

+0

@PeterM **注意:是!!! ** ***不是一個錯誤,不是一個致命的錯誤,甚至不是一個警告!!! ***但是什麼是價值? **「NULL」**但讓我編輯它爲雅,如果它讓你更快樂...... – Poiz

+0

@PeterM代碼現在包含一個簡單的三元操作構造:** isset($ _ SESSION ['ic'])? $ _SESSION ['ic']:null; **區別在於,它仍然是我的編輯...看到這一點? – Poiz

0

爲$ IC多次聲明

,如果你已經創建了一個會話,然後使用:

session_start(); 

,或者如果你想創建一個新的,使用:

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