php - 删除按钮未返回删除查询的正确值

标签 php mysql database

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

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

我的 table :

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

线路

回显“$id”;

当每行输出到屏幕时,foreach 循环成功返回每行的 id。

按下删除按钮后,该行

print_r($_POST);

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

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

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

问题 为什么“删除”按钮只继续传递最后一行的 id,而不是其所在行的 id?另外,我必须如何做才能做到这一点,以便当按下删除按钮时,仅传递该行旁边的删除按钮被删除了?

过去两个小时我一直在看这个,但仍然不明白我错在哪里;该代码显然正在工作,因为按下删除按钮时正在删除行,但只是删除了错误的行。

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

最佳答案

您永远不会关闭表单,因此将使用分配给 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 - 删除按钮未返回删除查询的正确值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17132092/

相关文章:

PHP - 字符串中的语句

mysql - 在网络上安装 mysql 服务器

c# - 同时针对云和自托管应用程序?

javascript - jquery 和 validate 在 laravel5 中点击时不起作用

PHP mysql_real_escape_string() 和 % 字符

MYSQL 结果顺序混合

java - MyBatis : query java. util.Date 与 MYSQL 数据库上的 DATE 类型

PHP 变量在 SQL 查询中不起作用?

sql - sql递归语句如何工作?

php - 如何在wordpress中通过ajax加载帖子的自定义字段值