重新显示包含更新数据的表单

时间:2012-04-07 13:38:36

标签: php

我对php很新,并且在使用upadated数据重新显示表单时遇到问题;

到目前为止的故事:

我有注册和登录页面,通过电子邮件地址识别“用户”;在登录时,用户被带到自己的“会员”页面,该页面将显示该人员持有的个人数据。然后,如果他们愿意,他们会被邀请编辑这些数据。这会将它们带到“更新”页面,完成后,数据库将通过(隐藏)“更新+交流”页面进行更新,然后返回到自己的“主页”页面。问题是我无法获取“成员”页面,然后在表单中重新显示更新的数据,也不能让表单本身重新显示。

以下是“成员”页面的相关代码:(我还没有编写任何安全性或验证 - 这些都在localhost上 - 我希望首先获得正确的编码)

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

我会得到'Successfull'和'View Result'。当我点击“查看结果”时,我会进入“会员”页面,但没有显示任何表格。

我将不胜感激。

1 个答案:

答案 0 :(得分:0)

页面开头缺少session_start()。 此外,在update_ac.php中,您将链接设为

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

因此,您应该使用它来将memeber页面中的用户字段设为$_GET['view'],然后使用它来进一步执行查询,因为$_SESSION不可用。