gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:29:10 25 4
gpt4 key购买 nike

我有一个表,它显示从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com