Link to home
Start Free TrialLog in
Avatar of v s
v s

asked on

How to Implement edit and delete button in PHP for each row fetched from mysql table ??

I want to display all data ( row wise ) of  a particular person (fetched from MYSQL table )  and implement edit and delete  button in front of each row.

i am attaching file of table whose data to be fetched and displayed
booking.PNG
Avatar of arnold
arnold
Flag of United States of America image

Within the php code, you need to add the or a link to the edit.php?id=rowid to match the record the edit form will edit.
Same would apply with a delete.php?id=rowid
Depending on your site design, the above is an example that should be thought through.

In short, presumably you use a foreach or while data from the query.........., deletion shoukd be marking a delete column with a 1 so it will not display versus actually deleting the row.

Much more logic, to avoid leaving orphan records...........

The location at the front, at the end, or after a specific column can all be controlled through the outputing/formatting of the data.
I make this php page that edit and hide the info of the users..Are you sure that you want to delete the items?If you delete them then you can't have the re-edit data.The code uses JQuery to fire the click events/
<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
        <style>
            table, th, td {
           border: 1px solid black;
           text-align: center;
}
            
        </style>
    </head>
    <body>
        <table style="width:100%">
  <tr>
    <th>1</th>
    <th>2</th> 
    <th>3</th>
    <th>4</th>
    <th>5</th> 
    <th>6</th>
    <th>7</th>
    <th>8</th> 
    <th>9</th>
    <th>10</th>
    <th>11</th>
  </tr>
  <?php
  //create connection
 $connect=mysqli_connect('localhost','root','','db_temp');
	
//check connection
 if(mysqli_connect_errno($connect))
 {
	echo 'Failed to connect to database: '.mysqli_connect_error();
}
else
	echo 'Connected Successfully!!';
						
$result=mysqli_query($connect,"select * from roombooking");

while($row=mysqli_fetch_array($result))
{
          $id=$row['Id'];
          $emp_id=$row['emp_id'];
          $room_id=$row['room_id'];
          $event=$row['event'];
          $booking_date=$row['booking_date'];
          $entry_time=$row['entry_time'];
          $exit_time=$row['exit_time'];
          $created_on=$row['created_on'];
          $dnt=$row['dnt'];
   echo "<tr data-id='$id'><td><button class='edit' data-id='$id'>Edit</button></td>"
           . "<td class='data'><div data-id='$id'>$id</div></td><td class='data'><div data-id='$id'>$emp_id</div></td><td class='data'><div data-id='$id'>$room_id</div></td><td class='data'><div data-id='$id'>$event</div></td>"
           . "<td class='data'><div data-id='$id'>$booking_date</div></td><td class='data'><div data-id='$id'>$entry_time</div></td>"
           . "<td class='data'><div data-id='$id'>$exit_time</div></td><td class='data'><div data-id='$id'>$created_on</div></td><td class='data'><div data-id='$id'>$dnt</div></td>"
           . "<td><button class='delete' data-id='$id'>Delete</button></td>"
           . "</tr> ";
          
 }	
  ?>
  
</table>
        <script>
        $(document).ready(function (){
          $('.delete').on('click',function (){
             var id=$(this).data('id');             
             $("div[data-id='" + id +"']").hide();
             
          });
          $('.edit').on('click',function (){
             var id=$(this).data('id');             
             $("div[data-id='" + id +"']").show();
          });
        });
        
        </script>
        
    </body>
</html>

Open in new window

I recently had to do this and used the below example at codexworld.com. You will need to sign up to download the example but it is excellent.
PHP CRUD Operations without Page Refresh using jQuery Ajax MySQL

You do not have to accept my comment as an answer.
Avatar of v s
v s

ASKER

@Leonidas Dosas
Thank you for the code.
I want delete that data from database not just hide (where the delete query will be written)
also how data will be edited.
Could you please help me for that??
Avatar of v s

ASKER

@Leonidas Dosas
waiting for your response.
have a nice day
Give me some time and I will fix this.For your info I am going to use ajax JQuery method to this.
Avatar of v s

ASKER

ok. thank you :)
@Leonidas Dosas
ASKER CERTIFIED SOLUTION
Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of v s

ASKER

@Leonidas Dosas

in the php while where css is defined??
class data, rowdata not defined..
If you see the post I created two different files. One .html and another one .php. The css settings are defined in the .html file inside style element. The classes are defined inside the .php file (php code) as it seems. With this way we have two goals. The functionality  of the table and the styling.