从表中删除用户

时间:2014-05-17 01:01:42

标签: php mysql forms

我有一个列出网站上所有用户的表格:

<?php $pt_table = '<form method="post" action="" onsubmit="return confirm(\'Are you sure you want to delete user?\');"><table id="patients">
    <tr>
        <th>Username</th>
        <th>User id</th>
        <th>Email</th>
        <th>Added on</th>
        <th>Group</th>
        <th></th>
    </tr>';


            $x=1;
            foreach ($users as $patient) {

                $pt_table .= '<tr';  if ($x % 2 == 0) {$pt_table .=' class="alt"';}
                $pt_table .= '>';
                $pt_table .=  '<td><a href="profile.php?username='.$patient['username'].'">'.$patient['username'].'</a></td>
                   <td>'.$patient['id'].'</td>
                   <td>'.$patient['email'].'</td>
                   <td>'.$patient['joined'].'</td>
                   <td>';
                if ($patient['group'] == 1) { $pt_table .= 'User';} elseif ($patient['group'] == 2) { $pt_table .= 'Administrator';}
                $pt_table .= '</td><td><input id="gobutton" type="submit" name="submit" value="Remove" style="margin-top: 5px; margin-left: 5px" /></td>';
                $pt_table .= '<input type="hidden" name="user_id" value="'.$patient['id'].'"';

                $pt_table .='</td></tr>';

                $x++;
            }
            $pt_table .='</table></form>';
            echo '<br><br/><br/><br/>';
            echo $pt_table;

我想在用户点击表格上的相应按钮删除该记录时删除特定记录。这是删除代码:

if (isset($_POST['submit']) && isset($_POST['user_id']) && $_POST['user_id'] !== '') {


            if ($admin->deleteuser($_POST['user_id']) == true) {
                header('Location:add-user.php?deleted');
            } else {
                $msg->add('e', 'The was a problem deleting user.');
            }
        }

问题是表中的最后一条记录被删除而不是用户按下删除按钮删除的记录。我做错了什么?

3 个答案:

答案 0 :(得分:1)

您的表单包含每个用户的所有形式。并重新使用每个name属性。因此,当表单发布到服务器时,它可能只使用name的最后一个表单元素(在这种情况下为user_id)。

基本上,你简化的HTML看起来像这样:

<form>
  <table>
    <tr>
      <td>
        <input ... />
        <input ... />
      </td>
    </tr>
    <tr>
      <td>
        <input ... />
        <input ... />
      </td>
    </tr>
    <!-- and so on... ->
  </table>
</form>

为了辨别每个用户,您可以将它们包装在各自的表单中。更像是这样:

<table>
  <tr>
    <td>
      <form>
        <input ... />
        <input ... />
      </form>
    </td>
  </tr>
  <tr>
    <td>
      <form>
        <input ... />
        <input ... />
      </form>
    </td>
  </tr>
  <!-- and so on... ->
</table>

这样每个表单只有一个隐藏input user_id和一个提交按钮。您可以通过修改发布的HTML来执行此操作,以便form标记位于循环内。请注意,整个form可能只在一个td中,我不认为在form周围添加tr标记是有效的HTML。

答案 1 :(得分:0)

您只有一个表单并且有多个

<input type="hidden" name="user_id" >

简单的解决方案,您可以像这样更改您的approche

将提交按钮更改为

<input   type="button"   value="Remove"
onclick="window.location.href=\'?deleteuser_id='.$patient['id'].'\'; "
 style="margin-top: 5px; margin-left: 5px" />

然后

if ( isset($_GET['deleteuser_id']) && $_GET['deleteuser_id'] !== '') {


            if ($admin->deleteuser($_GET['deleteuser_id']) == true) {
                header('Location:add-user.php?deleted');
            } else {
                $msg->add('e', 'The was a problem deleting user.');
            }
        }

答案 2 :(得分:-1)

更改

 $pt_table .= '<input type="hidden" name="user_id" value="'.$patient['id'].'"';

为:

 $pt_table .= '<input type="hidden" name="user_id" value="'.$patient['id'].'" />"';

关闭标记,值属性将有所帮助。

相关问题