Talha Mahmood Talha Mahmood - 2 months ago 5x
PHP Question

Why does my form delete the last row rather than the selected one?

In my code it first shows result of table and then it will delete the data upon submit. But there is issue that wherever I click in delete button I will always deleting last row. It does not delete specific row which I want to delete

$servername = "localhost";
$username = "root";
$password = "1234";
$db = "ptcl";
$con = new mysqli($servername, $username, $password, $db);

end connection

html code

<div class="content">
<div class="exchange">
<center>EXCHANGE /NTE</center>>
<form action="delete.php" name="delete" method="POST">
<table border="1">
<tr><th rowspan="2">EXCHANGE TYPE</th>
<th colspan="9">DESCRIPTION</th>
<td colspan="2">Retail</td>
<td colspan="2">Action</td>

form which i want to show first then delete by its id

global $con;
$query="select * from exchange ";
$result =$con->query($query);

if($result->num_rows > 0){

while($row = $result->fetch_assoc()){?>

<td><?php echo $row['exchangetype']?></td>
<td><?php echo $row['retail']?><td>
<td ><?php echo $row['dte']?></td>
<td><?php echo $row['retail_sett']?></td>
<td><?php echo $row['retail']+$row['dte']+$row['retail_sett']?></td>
<td><?php echo $row['offine']?></td>
<td><?php echo $row['retail']+$row['dte']+$row['retail_sett']+$row['offine']?></td>
<td><input type="submit" name="delete" value="Delete"/></td>
<td><input type="hidden" name="eid" value="<?php echo $row['eid']?>" />
<?php }
<?php } else{
echo "No record found";


php delete code starts

global $con

$eeid = $_POST['eid'];

if (isset($_POST['delete'])) {
$query = "DELETE FROM exchange WHERE eid='$eeid' ";

if ($con->query($query) === true) {
echo "DELETED Data";
} else {
echo "error during deletion" . $con->error;

it will not delete which i want to delete it delete only last row.


Hi Issue with your code is the hidden type you are using it always over write older values and set your last value as its value USE GET method to overcome the issue

<td><a href="delete.php?row_id=<?php echo $row['eid']?>" /></a></td>  

In delete.php or whatever your file name use this code

 $query="delete from exchange
       where eid='$eeid' ";

if ($con->query($query)===TRUE){
  echo "DELETED Data";
  echo "error during deletion".$con->error;