0

就这样吧。我有一个登录的网站。成功登录后,会创建一个名为 user 的会话变量,其中包含用户 ID、用户名、电子邮件等的数组。然后从那里我有到其他页面的链接。给我带来麻烦的是我有一个名为membership.php 的页面。此页面对用户 ID、用户名、电子邮件进行选择查询,并生成包含所有用户的表。每个用户旁边还有一个名为“编辑”的提交按钮。单击此按钮时,它会重定向到页面 edit_account.php。我的目标是当我单击编辑按钮时,会创建一个包含该特定用户的用户 ID 的会话变量。然后,当它重定向到 edit_account.php 页面时,我可以使用该会话作为我的选择语句的一部分从表中收集数据,然后编辑该用户的详细信息。

<?php 

// First we execute our common code to connection to the database and start the session 
require("common.php"); 

// At the top of the page we check to see whether the user is logged in or not 
if(empty($_SESSION['user'])) 
{ 
    // If they are not, we redirect them to the login page. 
    header("Location: ../../index.php"); 

    // Remember that this die statement is absolutely critical.  Without it, 
    // people can view your members-only content without logging in. 
    die("Redirecting to index.php"); 
} 

// We can retrieve a list of members from the database using a SELECT query. 
// In this case we do not have a WHERE clause because we want to select all 
// of the rows from the database table. 
$query = " 
    SELECT 
id,
        roleid, 
        username, 
        email 
    FROM user
"; 

try 
{ 
    // These two statements run the query against your database table. 
    $stmt = $db->prepare($query); 
    $stmt->execute(); 
} 
catch(PDOException $ex) 
{ 
    // Note: On a production website, you should not output $ex->getMessage(). 
    // It may provide an attacker with helpful information about your code.  
    die("Failed to run query: " . $ex->getMessage()); 
} 


// Finally, we can retrieve all of the found rows into an array using fetchAll 
$rows = $stmt->fetchAll(); 


if (isset($_POST['Edit'])) {


    $_SESSION['id'] = $_POST['id'];
    header("Location: edit_account.php");

}

?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Registration</title>
<link href="../../css/default.css" rel="stylesheet" type="text/css" />
</head>

<div id="container">
    <div id="header">
        <h1>

        </h1>
    </div>
    <div id="navigation">
        <ul>
            <li><a href="../adminindex.php">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">Contact us</a></li>
            <li><a href="logout.php">Logout</a></li>
        </ul>
    </div>
    <div id="content">
        <h2>
            Users
        </h2>
    <form action="" method="post">    
    <table border="0" align="left" cellpadding="25px">

        <tr> 
            <th>ID</th> 
            <th>Role ID</th> 
            <th>Username</th> 
            <th>E-Mail Address</th> 
        </tr> 

        <?php foreach($rows as $row): ?> 
            <tr> 
                <td><?php echo $row['id']; ?></td>
                <td><?php echo $row['roleid']; ?></td> <!-- htmlentities is not needed here because $row['id'] is always an integer --> 
                <td><?php echo htmlentities($row['username'], ENT_QUOTES, 'UTF-8'); ?></td> 
                <td><?php echo htmlentities($row['email'], ENT_QUOTES, 'UTF-8'); ?></td> 
                <td><input name="Edit" type="submit" value="Edit" /></td>
                <td><input name="id" type="hidden" value="<?php echo $row['id']; ?>" /></td>
            </tr> 
        <?php 
        endforeach; 
        ?>

         </tr>
     </table>  
     </form>

    </div>
    <div id="footer">
        Copyright ©  2013
    </div>
</div>



<body>
</body>
</html>

我相信问题出在代码块中:

    if (isset($_POST['Edit'])) {


    $_SESSION['id'] = $row['id'];
    header("Location: edit_account.php");

}

但是我尝试了很多东西,但似乎没有任何效果。同样在 edit_account.php 页面上,我在顶部有以下代码:

echo '<pre>';
var_dump($_SESSION);
echo '</pre>';

它吐出会话变量中的所有内容。当我选择提交按钮并重定向时,这是上述代码的输出。

array(2) {
 ["user"]=>
  array(4) {
    ["id"]=>
    string(1) "1"
    ["username"]=>
    string(5) "admin"
    ["roleid"]=>
    string(1) "1"
    ["email"]=>
    string(15) "admin@admin.com"
  }
  ["id"]=>
  NULL
}

预先感谢您的帮助。任何事情都非常感谢。

4

3 回答 3

0

主要问题是您基本上是在构建一个看起来像的表单(去掉所有绒毛 html):

<form>
<input name="Edit" type="submit" value="Edit" />
<input name="id" type="hidden" value="foo" />
<input name="Edit" type="submit" value="Edit" />
<input name="id" type="hidden" value="bar" />
<input name="Edit" type="submit" value="Edit" />
<input name="id" type="hidden" value="baz" />
etc...
</form>

只有一个表单,带有多个提交按钮,以及同名隐藏字段的多个副本。因此,PHP 将使用LAST隐藏id值来填充 $_POST 。PHP 无法判断点击了众多提交按钮中的哪一个,或者它应该尝试使用该id特定提交按钮旁边的值——这不是 HTTP 表单的工作方式。

你需要更多这样的东西:

<table>
<tr><td><form><input type="hidden" name="id" value="foo"><input type="submit"></form></td></tr>
<tr><td><form><input type="hidden" name="id" value="bar"><input type="submit"></form></td></tr>
<tr><td><form><input type="hidden" name="id" value="baz"><input type="submit"></form></td></tr>
etc..
</table>

请注意,现在每一行都有自己的表单,其中包含一个提交按钮和一个隐藏字段。这样,只有一个隐藏字段被提交,并且您将id在 PHP 代码中显示正确的值。

于 2013-05-16T18:39:53.537 回答
0

将表格代码放在每个表格行中,而不是整个表格中的单个表格。

另一个问题是您从管理员帐户登录,并且您正在更改管理员会话变量,因此为其声明另一个会话变量。

或者您也可以将更新代码放在提交表单的页面的开头,以便更新用户数据而不需要更改会话变量。

于 2013-05-16T18:54:14.833 回答
0

这很棒。谢谢 Marc B。正是我想要的。这是html代码:

        <?php foreach($rows as $row): ?> 
        <tr> 
            <td> <form action="" method="post"> <?php echo $row['id']; ?> </form> </td> 
            <td> <form action="" method="post"> <?php echo $row['roleid']; ?>  </form> </td>
            <td> <form action="" method="post"> <?php echo htmlentities($row['username'], ENT_QUOTES, 'UTF-8'); ?> </form> </td>  
            <td> <form action="" method="post"> <?php echo htmlentities($row['email'], ENT_QUOTES, 'UTF-8'); ?> </form> </td>  
            <td> <form action="" method="post"> <input name="Edit" type="submit" value="Edit" /> <input name="id" type="hidden" value="<?php echo $row['id']; ?>" /> </form> </td>

        </tr> 
    <?php endforeach; ?>

我可以使用以下方法成功设置会话:

if (isset($_POST['Edit'])) {

$_SESSION['id'] = $_POST['id'];
header("Location: edit_account.php");

}

但似乎我遇到了另一个问题:(我还想在每一行上添加一个删除按钮来删除该用户帐户。现在看起来是这样的:

<td> <form action="" method="post"> <input name="Delete" type="submit" value="Delete" /> <input name="id" type="hidden" value="<?php echo $row['id']; ?>" /> </form> </td>

使用的php代码是:

if (isset($_POST['Delete'])) {

// Everything below this point in the file is secured by the login system 

// We can retrieve a list of members from the database using a SELECT query. 
// In this case we do not have a WHERE clause because we want to select all 
// of the rows from the database table. 
$query = " 
    DELETE 
    FROM user
    WHERE
        id = :id 
"; 

// The parameter values 
$query_params = array( 
    ':id' => $_POST['id'] 
); 

try 
{ 
    // These two statements run the query against your database table. 
    $stmt = $db->prepare($query); 
    $result = $stmt->execute($query_params); 
}

catch(PDOException $ex) 
{ 
    // Note: On a production website, you should not output $ex->getMessage(). 
    // It may provide an attacker with helpful information about your code.  
    die("Failed to run query: " . $ex->getMessage()); 
} 


// Finally, we can retrieve all of the found rows into an array using fetchAll 
$rows = $stmt->fetch(); 


    // This redirects the user back to the members-only page after they register 
    header("Location: ../adminindex.php"); 

    // Calling die or exit after performing a redirect using the header function 
    // is critical.  The rest of your PHP script will continue to execute and 
    // will be sent to the user if you do not die or exit. 
    die("Redirecting to adminindex.php.php"); 


}

我的问题是重定向。当我单击删除按钮时,它实际上运行了查询,但之后它只是重定向到 memberlist.php 但页面是空白的!?为什么会发生这种情况?有什么我遗漏的吗?我尝试更改标题位置但没有成功。谢谢您的帮助!

于 2013-05-17T12:55:36.720 回答