dongyange1101 2013-05-16 18:19
浏览 47
已采纳

PHP MySQL设置会话从MySQL生成的表提交

So here is goes. I have a website that has a login. Upon a successful login, a session variable called user is created which contains an array of the userid, username, email and so on. Then from there I have links to other pages. What is giving me trouble is that I have a page called membership.php. This page does a select query for the userid, username, email and generates a table with all of the users. There is also a submit button beside each user that is entitled "Edit". When this button is clicked it redirects to a page edit_account.php. My goal here is when i click on the edit button, a session variable is created containing the userid of that specific user. Then when it redirects to the edit_account.php page I can use that session as part of my select statement to gather data from the table and then edit that users details. Below is a snipit of my code so you can see what I am talking about.

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

I believe the problem resides in the block of code:

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


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

}

But I have tried many things and nothing seems to work. Also on edit_account.php page I have this code at the top:

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

which spits out everything in the session variable. When I select the submit button and it redirects, this is the output of the above code.

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
}

Thank you in advance for the help. Anything is greatly appreciated.

  • 写回答

3条回答 默认 最新

  • dsl36367 2013-05-16 18:39
    关注

    The main problem is that you're basically building a form that looks (stripping out all the fluff 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>
    

    There's just ONE form, with multiple submit buttons, and multiple copies of the same hidden field with the same name. As such, PHP will use the LAST hidden id value to populate $_POST with. There is NO way for PHP to tell which of the many submit buttons was clicked, or that it should try to use the id value next to that one particular submit button - that's not how HTTP forms work.

    You need something more like this:

    <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>
    

    Note now EACH row has its OWN form, with one submit button and one hidden field within. This way, only that ONE hidden field is submitted, and you'll get the proper id value showing up in your PHP code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击