doulierong0334 2013-06-16 10:01 采纳率: 100%
浏览 20
已采纳

删除按钮不返回正确的值以删除查询

I have a table which displays data drawn from 2 different tables in mysql.The table has a delete button on each row which, when pressed, would send run a delete query which would delete the row, and all it's associated data from the database.

However,no matter which delete button i click, it is always the last row in the table that is deleted, and not the row which the delete button is in.

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

My code:

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

The line

echo"$id";

under the foreach loop successfully returns the id of each row correctly as each row is being output to the screen.

Upon pressing the DELETE Button, the line

print_r($_POST);

outputs all variables in $_POST, which gives the result

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

where x is the id of the last row in the table(E.g if there are 5 rows in the table, x=5)

QUESTION Why does the DELETE button keep passing the id of the last row only, and not the id of the row it's on?Also, how and what do i have to do to make it so that when the delete button is pressed, only the row beside the delete button is deleted?

I've been looking at this for the past 2 hours and still can't figure out where i'm wrong; the code is obviously working as rows are being deleted when the delete buton is pressed, however it's just that the WRONG rows are being deleted.

Any help would be greatly appreciated. Thanks

  • 写回答

5条回答 默认 最新

  • dpcj32769 2013-06-16 10:11
    关注

    You never close your form, so the last value assigned to ID is used.

    Each Delete button needs to be a seperate form

    Also please note that echo can use multiple lines

    //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'>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。