简体   繁体   中英

How to select a specific row from a table to edit or delete

I have implemented a booking system, part of the system involves allowing users to modify or delete a booking. I am able to retrieve bookings from the database based on user's session and id which is displayed in a html table. I have been having trouble selecting a specific row from the html table to edit or delete a booking. I want to be able to select a specific row from the html table which i will then be able to edit or delete that record.

The code below retrieves the bookings and displays it in a table

bookings.php

 $sqlquery = "SELECT * FROM bookings1,users WHERE bookings1.userid = users.userid AND users.username = '".$_SESSION['loggedInUser']."' "; 
  $results = $mysqli->query($sqlquery);

if ($results->num_rows > 0) {
while ($row = $results->fetch_assoc()){

echo'<form method = "POST" action = "editmot.php" >';

 echo'<tr>';

  echo "<td>". $row["booking_id"]. "</td>";

   echo "<td>". $row["Type"]. "</td>";

     echo "<td>". $row["BookingDate"]. "</td>";

      echo "<td>". $row["Timeslot"]. "</td>";

        echo "<td>". $row["Manufacture"]. "</td>";

           echo "<td>". $row["Model"]. "</td>";

            echo "<td>". $row["RegistrationNo"]. "</td>";

           echo "<td><a href = 'editmot.php' id ='".$row['booking_id']."'> Edit </td>";

       echo "<td><a href = 'delete.php' id = '".$row['booking_id']."'> Remove </td>";

      echo '</tr>';

         echo '</form>';

In the href attribute of the Edit Link, include the Booking_Id as a URL parameter. When the Edit page loads, pick up the booking_id from the URL parameter and fetch the other fields for edit. Like :

echo " <a href = 'editmot.php' . '?EditBookingId=' . $row['booking_id'] " 

and then so on and so forth concatenate other things.

Your final href URL should look like : ' http://editmot.php?EditBookingId=24798 ' or some such.

use this code to edit or delete

<a href="samefile.php?action=update&id=$row['booking_id']">for edit</a>

<a href="samefile.php?action=delete&id=$row['booking_id']">for delete</a>

if(isset($_REQUEST['action']))
 if($_REQUEST['action']=="update")
  update query
else if($_REQUEST['action']=="delete")
delete query

Instead of using the anchor tag, you could use a submit button and a hidden input field to hold the booking ID, since each booking detail (or row) is in a separate form. You could use something like this in each booking row:

echo "<td><input name='edit' type='submit' value='Edit'>"
. "<input type='hidden' name='booking_id' value='".$row['booking_id']."'></td>";

echo "<td><input name='delete' type='submit' value='Remove'>"
. "<input type='hidden' name='booking_id' value='".$row['booking_id']."'></td>";

And then in editmot.php , process the delete or edit form action:

if (isset($_POST['edit'])) {
    // edit video here
}

if (isset($_POST['delete'])) {
    // delete video here
}

You need to insert the id in the url so you can get it with the $_GET method in the edit or delete file.

So your anker needs to look something like:

<a href = 'delete.php?id=".$row['booking_id']."'>Delete</a>;

Now when a user go's to that url the url wil look something like:

localhost/delete.php?id=20

Now in your delete.php you can get the id with the $_GET method:

$id = $_GET['id']; $qry = "DELETE FROM booking1 WHERE id=$id"; //this will delete the row where id is 20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM