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