2012-04-07 61 views
0

我對PHP相當陌生,並且在重新顯示帶有更新數據的表單時遇到問題;重新顯示包含更新數據的表單

迄今爲止的故事:

我有一個電子郵件地址來標識一個「用戶」註冊和登錄頁面;在登錄時,用戶被帶到自己的「會員」頁面,該頁面將顯示該人員持有的個人數據。如果他們願意的話,他們會被邀請編輯這些數據。這將把他們帶到'更新'頁面,完成後,數據庫通過(隱藏)'update + ac'頁面更新,然後返回到他們自己的'主'頁面。問題是,我無法獲取「成員」頁面,然後在表單中重新顯示更新的數據,也無法將表單重新顯示。

下面是「成員」頁面培訓相關代碼:(我還沒有編碼的任何安全性或有效性尚未 - 部份都是在本地主機上 - 我想加時賽得到的編碼右第一)

<?php 
     include_once 'login.php'; 
     include_once 'functions.php'; 

     if (isset($_SESSION['user'])) 
      { 
      $user = $_SESSION['user'];   

     mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); 
     mysql_select_db($dbname) or die("Unable to connect: " . mysql_error()); 

     $data = "SELECT * FROM `names` WHERE email='$user'"; 
     $result=mysql_query($data) or die(mysql_error()); 
    while($row=mysql_fetch_array($result)){ 
    } 
?> 
    <table border='1px' style="background-color:#F0F8FF; font-weight: bold;" > 
     <caption>Personal Record</caption> 

    <tr> 
    <th>ID</th> 
     <td><?php 
      echo $row['id']; 
     ?></td> 
    </tr> 
    <tr> 
    <th>Name</th> 
     <td><?php 
      echo $row['name']; 
     ?></td> 
    </tr> 

    <tr> 
    <th>E-Mail</th> 
     <td><?php 
      echo $row['email']; 
     ?></td> 
    </tr> 

    <tr> 
    <th>Main Telephone</th> 
     <td><?php 
      echo $row['maintel']; 
     ?></td> 
    </tr> 
    <tr> 
    <th>Mobile Telephone</th> 
     <td><?php 
      echo $row['mobtel']; 
     ?></td> 
    </tr> 
    <tr> 
    <th>Organisation</th> 
     <td><?php 
      echo $row['organisation']; 
     ?></td> 
    </tr> 
    <tr> 
    <th>Group Leader</th> 
     <td><?php 
      echo $row['group_leader']; 
     ?></td> 
    </tr> 
    <tr> 
    <th>Supervisor</th> 
     <td><?php 
      echo $row['supervisor']; 
     ?></td> 
    </tr> 
    <tr> 
    <th>Volunteer</th> 
     <td><?php 
      echo $row['volunteer']; 
     ?></td> 
    </tr> 
    <tr> 
    <th>Assessor</th> 
     <td><?php 
      echo $row['assessor']; 
      } 

     ?></td> 
    </tr> 
    </table> 


     <p><br /> 
     <form method="post" action="update.php"> 
      <input name="Submit1" type="submit" value="Edit" style="width: 67px" /></form> 
     </p> 
     <p>&nbsp;</p> 

以下是該 'update.php' 頁面的代碼:

<?php 
include_once 'login.php'; 
     include_once 'functions.php'; 

    session_start(); 
// Connect to server and select database. 
    mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); 
     mysql_select_db($dbname) or die("Unable to connect: " . mysql_error()); 
// get value of id that sent from address bar 
      if (isset($_SESSION['user'])) 
      { 
      $user = $_SESSION['user']; 
// Retrieve data from database 
$sql="SELECT * FROM names WHERE email='$user'"; 
$result=mysql_query($sql); 
$rows=mysql_fetch_array($result); 
} 
?> 
<table width="400" border="0" cellspacing="1" cellpadding="0"> 
<tr> 
<form name="form1" method="post" action="update_ac.php"> 
<td> 
<table width="100%" border="0" cellspacing="1" cellpadding="0"> 
<tr> 
<td>&nbsp;</td> 
<td colspan="3"><strong>Update data in mysql</strong> </td> 
</tr> 
<tr> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
<td align="center">&nbsp;</td> 
</tr> 
<tr> 
<td align="center">&nbsp;</td> 
<td align="center"><strong>Name</strong></td> 
<td align="center"><strong>E-Mail</strong></td> 
<td align="center"><strong>Main Tel</strong></td> 
<td align="center"><strong>Mob Tel</strong></td> 
<td align="center"><strong>Organisation</strong></td> 
<td align="center"><strong>Group Leader</strong></td> 
<td align="center"><strong>Supervisor</strong></td> 
<td align="center"><strong>Volunteer</strong></td> 
<td align="center"><strong>Assessor</strong></td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td> 
<td><input name="email" type="text" id="email" value="<? echo $rows['email']; ?>" size="40"></td> 
<td align="center"><input name="maintel" type="text" id="maintel" value="<? echo $rows['maintel']; ?>" size="15"></td> 
<td align="center"><input name="mobtel" type="text" id="mobtel" value="<? echo $rows['mobtel']; ?>"></td> 
<td align="center"><input name="organisation" type="text" id="organisation" value="<? echo $rows['organisation']; ?>"></td> 
<td align="center"><input name="group_leader" type="text" id="group_leader" value="<? echo $rows['group_leader']; ?>"></td> 
<td align="center"><input name="supervisor" type="text" id="supervisor" value="<? echo $rows['supervisor']; ?>"></td> 
<td align="center"><input name="volunteer" type="text" id="volunteer" value="<? echo $rows['volunteer']; ?>"></td> 
<td align="center"><input name="assessor" type="text" id="assessor" value="<? echo $rows['assessor']; ?>"></td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td> 
<td align="center"><input type="submit" name="Submit" value="Submit"></td> 
<td>&nbsp;</td> 
</tr> 
</table> 
</td> 
</form> 
</tr> 
</table> 
<? 
// close connection 
mysql_close(); 
?> 

最後,這裏是爲'upate_ac.php頁面代碼:

<?php 
     include_once 'login.php'; 
     include_once 'functions.php'; 
// Connect to server and select database. 
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); 
     mysql_select_db($dbname) or die("Unable to connect: " . mysql_error()); 
// update data in mysql database 
$id = $_POST['id']; 
$name = $_POST['name']; 
$email = $_POST['email']; 
$maintel = $_POST['maintel']; 
$mobtel = $_POST['mobtel']; 
$organisation = $_POST['organisation']; 
$group_leader = $_POST['group_leader']; 
$supervisor = $_POST['supervisor']; 
$volunteer = $_POST['volunteer']; 
$assessor = $_POST['assessor']; 
$sql="UPDATE `names` SET id='$id', `name`='$name', `email`='$email', `maintel`='$maintel', `mobtel`='$mobtel', `organisation`='$organisation', `group_leader`='$group_leader', `supervisor`='$supervisor', `volunteer`='$volunteer', `assessor`='$assessor' WHERE `id`='$id''"; 
$result=mysql_query($sql); 
// if successfully updated. 
if($result){ 
echo "Successful"; 
echo "<BR>"; 
echo "<a href='member.php?view=$user'>View result</a>"; 
} 
else { 
echo "ERROR"; 
} 
?> 

會發生什麼,我會得到'成功'和'查看結果'。當我點擊'查看結果'時,我會進入'會員'頁面,但沒有顯示任何表格。

我將不勝感激任何幫助。

問候,

'H'

+0

_(我還沒有編寫任何安全性或驗證 - 所有都在本地主機上 - 我想首先得到編碼的權利_一定不要忘記 - 好的做法是驗證和逃脫,因爲你去.... (大多數情況下,這個評論是爲了那些會錯過你的筆記在這個問題上的人,並且在這裏跳到關於SQL注入的問題上) – 2012-04-07 13:41:48

+0

你應該使用正確的代碼縮進和你的html中的單獨代碼邏輯。 – 2012-04-07 13:54:43

回答

0

session_start()缺少在頁面的開始。 此外,在update_ac.php你把鏈接作爲

<a href='member.php?view=$user'>View result</a>". 

因此,無論你應該使用它來獲取memeber頁面的用戶字段$_GET['view'],然後用它來進一步執行查詢,$_SESSION不可用。

+0

Thanks for this! – Hamish 2012-04-07 14:05:34

+0

我在一些較早的代碼中使用了'session_start();'但是還有另外一個問題:當用戶首先註冊並且然後登錄表格時會正確地顯示它們的值;隨後登錄介紹性段落(在html中)顯示錶格沒有,所以一旦表格沒有編輯它們的值再次重複。這似乎是一次性行動,我認爲這是問題 - 有些東西阻止了表格的重新顯示。 – Hamish 2012-04-07 14:09:48