Talha Mahmood Talha Mahmood - 1 year ago 56
SQL Question

not deleted selected row data. always delete last row plz fix this issue as soon as possible

in my code its first show result of table and then it will delete the data by submit. but there is issue that wherever i click in delete button i will always deleting last row wise. not deleting 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.

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download