2017-03-08 2 views
1

私は各データベースエントリに対して編集ボタンを持っていたいと思います。ユーザーが編集をクリックすると、それだけでエントリが取得されます。私は<input type="hidden" name="userid" value=...>のような隠された入力を使用しようとしましたが、実際にどこに置くべきか、それがどのように役立つかはわかりません。ボタンクリックでIDを渡すユーザーごと

それも視覚化することができるので、私はテーブルの写真を添付し​​ました。

enter image description here

私はあなたが私を助けることを願って、あなたが知っている必要があり、他の情報がありますならば、ちょうど私がしてください知っています。

<?php 
include 'dbConnect.php'; 
echo '<table border="1" cellpadding="1" cellspacing="1" width="90%" style="margin: 0 auto">'; 
echo '<tr class="allUsers">'; 
echo '<th>Username</th>'; 
echo '<th>First name</th>'; 
echo '<th>Last name</th>'; 
echo '<th>Email</th>'; 
echo '<th>Year group</th>'; 
echo '<th>Subject 1</th>'; 
echo '<th>Subject 2</th>'; 
echo "<th>Subject 1's teacher</th>"; 
echo "<th>Subject 2's teacher</th>"; 
echo '<th>Privilege</th>'; 
echo '<th></th>'; 
echo '</tr>'; 
$getUsers = mysqli_query($connection, "SELECT * FROM users");   
while($users=mysqli_fetch_assoc($getUsers)){ 
    echo '<form method="post" action="">'; 
    echo '<tr>'; 
    echo '<td>'.$users['username'].'</td>'; 
    echo '<td>'.$users['first_name'].'</td>'; 
    echo '<td>'.$users['last_name'].'</td>'; 
    echo '<td>'.$users['email'].'</td>'; 
    echo '<td>'.$users['year_group'].'</td>'; 
    echo '<td>'.$users['subject'].'</td>'; 
    echo '<td>'.$users['subject2'].'</td>'; 
    echo '<td>'.$users['teacher'].'</td>'; 
    echo '<td>'.$users['teacher2'].'</td>'; 
    echo '<td>'.$users['is_admin'].'</td>'; 
    echo '<td><button name="editUser">Edit</button></td>'; 
    echo '</tr>'; 
    echo '</form>'; 
} 
if (isset($_POST['editUser'])){ 
    $username = $_SESSION['name']; 
    // .... 
} 
} 
echo '</table>'; 
?> 
+0

あなたは右のあなたのテーブルでそれを編集したり、別のページにユーザーを持って帰りたいと思いますか? – maximedubois

+0

同じページで、おそらく他のエントリを隠すか、それが簡単な場合は別のページだけを隠すことによって。 – leveeee

答えて

1

1.メソッド(隠された入力)

あなたが提出することができるすべての<form></form>に隠し入力を置くことができます。

<input type="hidden" name="userid" value=...> 

2.方法($_SESSION

第二の方法は、セッション中IDを保存することです。

利点:

  • あなたが提出することができるすべてのフォームに貼り付けする必要はありません
  • を操作することは容易ではありません。次のページにユーザーをリダイレクトするために




、編集する行に関する情報のいくつかの種類を送信するために役立つだろう。送信する情報が毎回異なる場合は、非表示の入力を使用できます。同じ行のIDをセッションに保存することができます。

+0

私は現時点で問題になっているのは、ユーザーIDが最後のユーザーのIDを常に表示していることです。どのようにこれを変更することができますので、ボタンをクリックして、それは正しいIDを取得?申し訳ありません私は現時点で何をしているのか分かりません。 – leveeee

+0

@leveeeeだから、ユーザーIDは毎回違うのですか?あるいは、各列自体のIDを意味しましたか? –

+0

はい、すべてが異なるIDを持っています。 – leveeee

1

ルカの答えから2番目の方法です。

getメソッドを使用して別のページの詳細を編集することもできます。 echo '<td><a herf="edit.php?id=' . urlencode(base64_encode($users['id'])) . '">Edit</a></td>';私たちはedit.phpでIDを取得して続行します。

<?php 
include 'dbConnect.php'; 
echo '<table border="1" cellpadding="1" cellspacing="1" width="90%" style="margin: 0 auto">'; 
echo '<tr class="allUsers">'; 
echo '<th>Username</th>'; 
echo '<th>First name</th>'; 
echo '<th>Last name</th>'; 
echo '<th>Email</th>'; 
echo '<th>Year group</th>'; 
echo '<th>Subject 1</th>'; 
echo '<th>Subject 2</th>'; 
echo "<th>Subject 1's teacher</th>"; 
echo "<th>Subject 2's teacher</th>"; 
echo '<th>Privilege</th>'; 
echo '<th></th>'; 
echo '</tr>'; 
$getUsers = mysqli_query($connection, "SELECT * FROM users"); 
while ($users = mysqli_fetch_assoc($getUsers)) { 

    echo '<tr>'; 
    echo '<td>' . $users['username'] . '</td>'; 
    echo '<td>' . $users['first_name'] . '</td>'; 
    echo '<td>' . $users['last_name'] . '</td>'; 
    echo '<td>' . $users['email'] . '</td>'; 
    echo '<td>' . $users['year_group'] . '</td>'; 
    echo '<td>' . $users['subject'] . '</td>'; 
    echo '<td>' . $users['subject2'] . '</td>'; 
    echo '<td>' . $users['teacher'] . '</td>'; 
    echo '<td>' . $users['teacher2'] . '</td>'; 
    echo '<td>' . $users['is_admin'] . '</td>'; 
    echo '<td><a herf="edit.php?id=' . urlencode(base64_encode($users['id'])) . '">Edit</a></td>'; 
    echo '</tr>'; 

} 
echo '</table>'; 
?> 

个人设定

<?php 

    if(isset($_GET['id']) && !empty($_GET['id'])){ 

    $id = urldecode(base64_decode($_GET['id'])); 


    // do queries for $id 

    }else{ 

     // id does not exits do something 

    } 
関連する問題