Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为每个用户在单击按钮时传递ID_Php_Mysql_Button_Input - Fatal编程技术网

Php 为每个用户在单击按钮时传递ID

Php 为每个用户在单击按钮时传递ID,php,mysql,button,input,Php,Mysql,Button,Input,我希望每个数据库条目都有一个编辑按钮,当用户单击编辑时,它只会得到条目,就在它旁边。我尝试过使用一个隐藏的输入,比如,但我真的不知道该把它放在哪里,以及它能起到什么作用 所以你也可以想象一下,我附上了一张桌子的照片 我希望你能帮助我,如果有任何其他信息,你需要知道,请告诉我 <?php include 'dbConnect.php'; echo '<table border="1" cellpadding="1" cellspacing="1" width="90%" style=

我希望每个数据库条目都有一个编辑按钮,当用户单击编辑时,它只会得到条目,就在它旁边。我尝试过使用一个隐藏的输入,比如
,但我真的不知道该把它放在哪里,以及它能起到什么作用

所以你也可以想象一下,我附上了一张桌子的照片

我希望你能帮助我,如果有任何其他信息,你需要知道,请告诉我

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

1。方法(隐藏输入)

您可以将隐藏的输入放入每个可以提交的

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

2。方法(
$\u会话

第二种方法是在会话中保存
ID


优点:

  • 操纵起来并不那么容易
  • 您不需要将其粘贴到每个可以提交的表单中。



为了将用户重定向到下一页,发送一些关于要编辑的行的信息会很有帮助。如果每次发送的信息不同,则可以使用隐藏输入。每行的ID相同,可以保存在会话中。

Luca答案中的第二种方法。

您还可以使用get方法在另一个页面上编辑详细信息<代码>回显“编辑”我们只需要在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

  }

您想在您的表中编辑它或将用户带到另一个页面?在同一页面上,可能是通过隐藏其他条目,或者如果更简单的话,只需另一个页面。目前的问题是,用户id总是显示最后一个用户的id。如何更改这一点,因此通过单击按钮,它可以获得正确的id?对不起,我不知道我现在在做什么。@Leveee所以用户id每次都不同?或者您是指每一行本身的id吗?是的,它们都有不同的id。会话不是最好的方法。如果登录用户是admin@leveeee好的,那么我会向你推荐你在问题中提到的方法1。但不要将ID保存为纯文本,请使用安全哈希算法为相同的值创建动态哈希。