【问题标题】:Delete button not returning correct values to delete query删除按钮未返回正确的值以删除查询
【发布时间】:2013-06-16 10:01:07
【问题描述】:

我有一个表,它显示从 mysql 中的 2 个不同表中提取的数据。该表的每一行都有一个删除按钮,当按下该按钮时,将发送运行删除查询,该查询将删除该行,以及它的所有相关数据来自数据库。

但是,无论我单击哪个删除按钮,删除的始终是表中的最后一行,而不是删除按钮所在的行。

我的桌子:

-------------------------------------------
|Username|Password|Branch Name|Branch Info|
----------------------------------------------------
|Sub1    |Pass1   |branch1    |1st branch |DELETE  |
----------------------------------------------------
|Sub2    |Pass2   |branch2    |2nd branch |DELETE  |<---this button is pressed
----------------------------------------------------
|Sub3    |Pass3   |branch3    |3rd branch |DELETE  |
----------------------------------------------------
|Sub4    |Pass4   |branch4    |4th branch |DELETE  |
----------------------------------------------------
|Sub5    |Pass5   |branch5    |5th branch |DELETE  |<---this row is deleted.
----------------------------------------------------

我的代码:

<?PHP
session_start();

if(@$_SESSION['Auth']!=="Yes" AND @$_SESSION['Type']!=="Admin")
{
echo"You are not authorised to view this page.Please click <a href='Login.php'>here</a> to login.";
exit();
}
?>

<?PHP

if(@$_POST['deluser']=="Delete")
{
include("cxn.inc");
print_r($_POST);
$id="$_POST[id]";
echo"$id";
if(empty($_POST["id"]))
echo"yep";
$deluser="DELETE FROM SubUsers WHERE SubUsers.id='$id'";
$delquery=mysqli_query($cxn,$deluser) or die (mysqli_error($cxn));
$delsuccess="Deletion successful";
}
>

<html>
<head><link rel="stylesheet" type="text/css" href="style.css" /></head>
<body>


<?PHP
include("cxn.inc");
//include("AdminNav.php");
$viewuser="SELECT SubUsers.Id,Username,Password,Name,Info FROM SubUsers,Branch WHERE SubUsers.id=Branch.id AND SubUsers.Userid='$_SESSION[UserId]'";
$runuser=mysqli_query($cxn,$viewuser) or die(mysqli_error($cxn));
$rows=array();
while($row=mysqli_fetch_assoc($runuser))
$rows[]=$row;
echo"<form action='$_SERVER[PHP_SELF]' name='DelUser' method='POST' >";
echo"<table border='1'>";
echo"<tr>";
echo"<td>";
echo"Username";
echo"</td>";
echo"<td>";
echo"Password";
echo"</td>";
echo"<td>";
echo"Branch Name";
echo"</td>";
echo"<td>";
echo"Branch Info";
echo"</td>";
echo"</tr>";
foreach($rows as $row)
{
$id=$row['Id'];
echo"$id";
echo"<tr>";
echo"<td>";
echo"$row[Username]";
echo"</td>";
echo"<td>";
echo"$row[Password]";
echo"</td>";
echo"<td>";
echo"$row[Name]";
echo"</td>";
echo"<td>";
echo"$row[Info]";
echo"</td>";
echo"<td>";
echo"<input type='hidden' name='id' value='$id' >";
echo"<input type='Submit' name='deluser' value='Delete' >";
echo"</td>";
echo"</tr>";
}
echo"<tr>";
echo"<td colspan='5'>";
if(isset($delsuccess))
{echo"$delsuccess";}

echo"</td>";
echo"</tr>";
echo"</table>";
echo"</form>";
?>
</body>
</html>

线

echo"$id";

在 foreach 循环下,每行都在输出到屏幕时成功地正确返回了每一行的 id。

按下 DELETE 按钮后,行

print_r($_POST);

输出$_POST中的所有变量,给出结果

Array ( [id] => x [deluser] => Delete ) 

其中 x 是表中最后一行的 id(例如,如果表中有 5 行,则 x=5)

问题 为什么 DELETE 按钮只传递最后一行的 id,而不是它所在行的 id?另外,我该怎么做才能做到这一点,以便在按下删除按钮时,只有该行旁边的delete按钮被删除了吗?

过去 2 小时我一直在看这个,但仍然无法弄清楚我错在哪里;当按下删除按钮时,代码显然正在工作,因为正在删除行,但是只是删除了错误的行。

任何帮助将不胜感激。 谢谢

【问题讨论】:

  • html(按钮)的其余部分在哪里
  • @Gunr Jesra 你是什么意思?

标签: php mysql database


【解决方案1】:

您永远不会关闭表单,因此使用分配给 ID 的最后一个值。

每个删除按钮都需要是一个单独的表单

另外请注意,echo 可以使用多行

//Remove start of form from here.
echo"<table border='1'>";
echo"<tr>";
echo"<td>";
echo"Username";
echo"</td>";
echo"<td>";
echo"Password";
echo"</td>";
echo"<td>";
echo"Branch Name";
echo"</td>";
echo"<td>";
echo"Branch Info";
echo"</td>";
echo"</tr>";
foreach($rows as $row)
{
$id=$row['Id'];
echo"$id";
echo"<tr>";
echo"<td>";
echo"$row[Username]";
echo"</td>";
echo"<td>";
echo"$row[Password]";
echo"</td>";
echo"<td>";
echo"$row[Name]";
echo"</td>";
echo"<td>";
echo"$row[Info]";
echo"</td>";
echo"<td>";
    //form isnt needed till here.
    echo"<form action='$_SERVER[PHP_SELF]' name='DelUser' method='POST' >";
echo"<input type='hidden' name='id' value='$id' >";
    //Line below has changed.
echo"<input type='Submit' name='deluser' value='Delete' ></form>";
echo"</td>";
echo"</tr>";
}
echo"<tr>";
echo"<td colspan='5'>";

【讨论】:

  • 感谢您的帮助托比,它现在正在工作。只是一个简单的问题,为什么每个删除按钮都必须是一个单独的表单?或者更确切地说,为什么每个提交按钮都必须是各自独立的形式?我的印象是我可以在 PHP 中的一个表单中有多个提交按钮,因为我有一个包含多个输入字段的注册表单,尽管只有一个提交按钮。
  • @Ken 我在回答中解释了
  • 这就是 HTML 表单的工作方式。您绝对可以有多个提交按钮和多个 ID 值,但它们都将被提交,因此您需要将单击的提交表单与您的 ID 链接,您需要唯一的 ID。拥有单独的表单要容易得多。
【解决方案2】:

提交按钮发送整个表单,而不仅仅是最近的输入。因此,您正在发送表中的每个 id 和按下按钮的值。由于每个隐藏的输入都有一些名称,$_POST 中的变量被覆盖并等于最后一个值。

您可以为每一行制作单独的表单以确保只发送一个 id(这可能会使您的 html 更大),或者从隐藏输入中退出并更改您的删除按钮名称以包含所需的 id:

echo "<input type='submit' name='deluser[$id]' value='Delete'>";

这样,您的帖子将如下所示:

$_POST['deluser'] = array(7 => 'Delete');

只会发送来自按下按钮的值。您可以在 php 中轻松访问此 id:

if (isset($_POST['deluser'])) {
    $id_to_delete = key($_POST['deluser']);
}

如果不关心IE版本太高的用户,可以使用button元素代替提交:

echo "<button type='submit' name='deluser' value='$id'>Delete</button>";

这样,$_POST['deluser'] 就可以访问 id。

【讨论】:

    【解决方案3】:

    如果我理解正确,您将在隐藏字段 id 上有 n(n 是记录数)声明。这可能只需要最后一个。因此,要使其正常工作,您需要确保选择了正确的隐藏字段。

    要么将其添加到删除函数中,要么创建具有合并 id 的字段。

    【讨论】:

      【解决方案4】:

      所有 id 都有一个表格

      <?PHP
      include("cxn.inc");
      //include("AdminNav.php");
      
      ?>
      
      
      <table border='1'>
      <tr>
      <td>Username</td>
      <td>Password</td>
      <td>Branch Name</td>
      <td>Branch Info</td>
      </tr>
      
      
      <?php
      $viewuser="SELECT SubUsers.Id,Username,Password,Name,Info FROM SubUsers,Branch WHERE SubUsers.id=Branch.id AND SubUsers.Userid='$_SESSION[UserId]'";
      $runuser=mysqli_query($cxn,$viewuser) or die(mysqli_error($cxn));
      while($row=mysqli_fetch_assoc($runuser)){
      $id=$row['Id'];
      
      echo"<form action='$_SERVER[PHP_SELF]' name='DelUser' method='POST' >";
      echo"<tr>";
      echo"<td>";
      echo"$row[Username]";
      echo"</td>";
      echo"<td>";
      echo"$row[Password]";
      echo"</td>";
      echo"<td>";
      echo"$row[Name]";
      echo"</td>";
      echo"<td>";
      echo"$row[Info]";
      echo"</td>";
      echo"<td>";
      echo"<input type='hidden' name='id' value='$id' >";
      echo"<input type='Submit' name='deluser' value='Delete' >";
      echo"</td>";
      echo"</tr>";
      echo"</form>";
      echo"<tr>";
      echo"<td colspan='5'>";
      if(isset($delsuccess))
      {echo"$delsuccess";}
      echo"</td>";
      echo"</tr>";
      }
      ?>
      </table>
      

      【讨论】:

        【解决方案5】:

        尝试根据选择的id删除,这将解决您的问题。例如,如果你想删除 sub2,它会在 db 中有 id。例如:-

        <form name="form3" id="form3" action="delete_xyz.php" method="get" onsubmit="return check();">
              <input type="image" src="../images/delete.png" type="submit" id="submit" value="Delete"  title="Deletes Selected Product" style="cursor:pointer" />
              <input type="hidden" name="delete" id="delete"  value="<?php echo $row['Complain_id']; ?>"/>
        </form>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-03
          • 2015-03-28
          • 2016-07-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多