2012-04-07 59 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