0

だからここに行きます。ログインできるWebサイトがあります。ログインに成功すると、userid、username、email などの配列を含む user というセッション変数が作成されます。そこから、他のページへのリンクがあります。問題を引き起こしているのは、membership.php というページがあることです。このページは、ユーザー ID、ユーザー名、電子メールの選択クエリを実行し、すべてのユーザーを含むテーブルを生成します。各ユーザーの横には、「編集」という資格を持つ送信ボタンもあります。このボタンをクリックすると、ページ edit_account.php にリダイレクトされます。ここでの私の目標は、編集ボタンをクリックすると、その特定のユーザーのユーザー ID を含むセッション変数が作成されることです。次に、edit_account.php ページにリダイレクトされたら、そのセッションを 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>';

セッション変数のすべてを吐き出します。送信ボタンを選択してリダイレクトすると、これが上記のコードの出力です。

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>

複数の送信ボタンを備えた 1 つのフォームと、同じ名前の同じ非表示フィールドの複数のコピーがあります。そのため、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>

ここで、各行には1 つの送信ボタンと 1 つの非表示フィールドを含む独自のフォームがあることに注意してください。このようにして、その 1 つの隠しフィールドだけ送信idされ、PHP コードに適切な値が表示されます。

于 2013-05-16T18:39:53.537 に答える
0

これは素晴らしい。ありがとう、マーク 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 に答える
0

テーブル全体ではなく、テーブルの各行にフォーム コードを配置します。

別の問題は、管理者アカウントからログインし、管理者セッション変数を変更しているため、別のセッション変数を宣言することです。

または、フォームが送信されるページの先頭に更新コードを配置して、セッション変数を変更する必要がないようにユーザーデータを更新することもできます。

于 2013-05-16T18:54:14.833 に答える