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 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP