【问题标题】:PHP MySQL setting session on submit from table generated by MySQL从 MySQL 生成的表提交 PHP MySQL 设置会话
【发布时间】:2013-05-17 20:55:36
【问题描述】:

就这样吧。我有一个登录的网站。成功登录后,会创建一个名为 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
}

提前感谢您的帮助。任何事情都非常感谢。

【问题讨论】:

  • 确保您在每个页面上都调用session_start()
  • 你能调试一下吗?使用var_dump 并确保您认为的内容确实存在。我还想看看像 fiddler 这样的工具,它可以让您轻松检查请求。
  • 好的,因此 session_start() 在 common.php 中被调用,这很好,否则它不会获取“用户”会话。我使用 var_dump 作为会话变量。我知道这是因为[“id”] => NULL而起作用。我称我的会话为“id”。此外,如果我硬编码一个数字,例如 $_SESSION['id'] = 2;这有效,它显示为 2。

标签: php session-variables mysql


【解决方案1】:

主要问题是您基本上是在构建一个看起来(去掉所有绒毛 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>

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

【讨论】:

  • 谢谢。请参阅我对这个问题的“答案”。让我知道你的想法
  • 您不需要在每个表格单元格中都有&lt;form&gt;&lt;form&gt;foo&lt;/form&gt; 毫无意义。没有表单字段,因此无需提交任何内容。
  • 好的,我明白了,谢谢。你知道为什么我会重定向到一个空白页面吗?
【解决方案2】:

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

另一个问题是你从管理员帐户登录,你正在更改管理会话变量,所以为它声明另一个会话变量。

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

【讨论】:

    【解决方案3】:

    这很棒。谢谢 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 但页面是空白的!?为什么会发生这种情况?有什么我遗漏的吗?我尝试更改标题位置但没有成功。感谢您的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-08
      • 2016-12-05
      • 2018-05-25
      • 1970-01-01
      • 2014-05-01
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多