-1

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

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

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

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

    if($result->num_rows > 0){


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

<tr>
    <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']?>" />  
    </td>
    <?php } 
    ?>
</tr>
</table>
</form>
    </center>
 <?php  } else{
echo "No record found";
        }

   ?>       
</div>
   </div>

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.

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

1

The problem is that you're repeating the hidden input in the form, and they all have the same name. When you submit the form, $_POST['eid'] will just be the last one.

You need to have a separate form for each row, not one form for the entire table, so that the submit button will just submit that one eid.

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

<tr>
    <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><form method='post' action="delete.php">
            <input type="submit" name="delete" value="Delete"/>
            <input type="hidden" name="eid" value="<?php echo $row['eid']?>" />  
        </form></td>
    <?php } 

Another option would be to put the EID into the submit button's value, rather than a hidden field.

<td><input type="submit" name="delete" value="<?php echo $row['eid'] ?>">/</td>

Then delete.php would do:

$eeid = $_POST['delete'];
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • ... or, for the sake of completeness, the OP could use [array fields](http://stackoverflow.com/a/3314578/472495). – halfer Aug 02 '16 at 18:59
  • @halfer How would he know which one's submit button was clicked on? – Barmar Aug 02 '16 at 19:01
  • The number in the square brackets e.g. `` is usually the primary key of the row being rendered. This will be transformed by PHP as a nested array e.g. `[ 'item' => [ 1 => 'value' ] ]`. – halfer Aug 02 '16 at 19:03
  • @halfer When he submits the form, `$_POST` will contain *all* the `` values. – Barmar Aug 02 '16 at 19:07
  • OK, he/she can do this: `` without the hidden field (and obviously varying the number to whatever PK for each row). PHP will populate the array as something like `[ 'delete' => [5] ]` depending on the delete button clicked on. – halfer Aug 02 '16 at 19:10
  • 1
    @halfer Actually, there's a simpler method like that. I just updated the answer with it. – Barmar Aug 02 '16 at 19:16
0

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>  
 </td>

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

//mysql_connection
if(isset($_GET['row_id'])){
 $eeid=$_GET['row_id'];
 $query="delete from exchange
       where eid='$eeid' ";

if ($con->query($query)===TRUE){
  echo "DELETED Data";
  }
  else{
  echo "error during deletion".$con->error;
  }
 }
} 
Sanooj T
  • 1,317
  • 1
  • 14
  • 25
  • bro in updation i wil do same thing but it will empty them when i update it. – Talha Mahmood Aug 01 '16 at 10:27
  • ` if(isset($_GET['row_id'])){ $eeid=$_GET['row_id']; $exchangetype=$_POST['exchangetype']; $retail=$_POST['retail']; $dte=$_POST['dte']; $retail_sett=$_POST['retail_sett']; $offine=$_POST['offine']; $query="Update exchange SET exchangetype='$exchangetype', retail='$retail', dte='$dte', retail_sett='$retail_sett', offine='$offine' where eid='$eeid'";` – Talha Mahmood Aug 01 '16 at 10:30
  • one minute i am checking – Sanooj T Aug 01 '16 at 10:39
  • That's not the way to do updation – Sanooj T Aug 01 '16 at 10:40
  • but its work and it will empty all the data when i click on hyperlink of update . in the field i use same name as above. – Talha Mahmood Aug 01 '16 at 10:45
  • pass your id to any page and select all data from mysql using that id and display it .use a hidden type to store the id and change values and use submit to update.when you update use your hidden id to update. – Sanooj T Aug 01 '16 at 10:46
  • Sir its not working or may be I don't understand because I'm beginner in this field . last question can I use form??? and can i use 2 ids?? and using isset which id name i use? (update or id )? thanks. – Talha Mahmood Aug 01 '16 at 11:16
  • hi check this tutorial http://www.freezecoders.com/2012/11/add-list-edit-delete-record-in-database-using-php.html – Sanooj T Aug 01 '16 at 11:24