gpt4 book ai didi

PHP MySQL 设置 session 从 MySQL 生成的表提交

转载 作者:行者123 更新时间:2023-11-30 01:37:58 24 4
gpt4 key购买 nike

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

<?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>';

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

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
}

预先感谢您的帮助。任何事情都会受到极大的赞赏。

最佳答案

主要问题是您基本上正在构建一个看起来像的表单(去掉所有多余的 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>

请注意,现在每一行都有其自己的表单,其中有一个提交按钮和一个隐藏字段。这样,只有 ONE 隐藏字段被提交,您就会在 PHP 代码中获得正确的 id 值。

关于PHP MySQL 设置 session 从 MySQL 生成的表提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16594650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com