2017-03-08 22 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?對不起,我不知道我現在在做什麼。 – leveeee

+0

@leveeee所以用戶ID每次都不一樣?或者你是否指每行本身的ID? –

+0

是的,他們都有不同的ID。 – leveeee

1

盧卡的答案的第二種方法。

您還可以使用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>'; 
?> 

edit.php

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

    } 
要編輯它的權利在你的表格或將用戶帶到另一個頁面