0

我对php相当陌生,并且在重新显示带有更新数据的表单时遇到问题;

到目前为止的故事:

我有通过电子邮件地址识别“用户”的注册和登录页面;在登录时,用户被带到他们自己的“会员”页面,该页面将显示该人持有的个人数据。然后,如果他们愿意,他们会被邀请编辑该数据。这会将他们带到“更新”页面,完成后,数据库会通过(隐藏的)“更新+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";
}
?>

发生的事情是我得到“成功”和“查看结果”。当我点击“查看结果”时,我会进入“会员”页面,但没有显示任何表格。

我将不胜感激任何帮助。

4

1 回答 1

0

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

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

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

于 2012-04-07T13:48:50.163 回答