html - Adding a delete button in PHP on each row of a MySQL table -
i trying add delete button on each row can delete record when button pressed. new php , mysql , stack overflow.
below table extract information mysql database , works.
<table class="table" > <tr> <th> staff id </th> <th> staff name </th> <th> class </th> <th> action </th> </tr> <?php while($book = mysqli_fetch_assoc($records)){ echo "<tr>"; echo "<td>".$book['staff_id']."</td>"; echo "<td>".$book['staff_name']."</td>"; echo "<td>".$book['class']."</td>"; echo "</tr>"; }// end while loop
simply using php follows (you can use js)
while($book = mysqli_fetch_assoc($records)){ echo "<tr>"; echo "<td>".$book['staff_id']."</td>"; echo "<td>".$book['staff_name']."</td>"; echo "<td>".$book['class']."</td>"; echo "<td><a href='delete.php?id=".$book['staff_id']."'></a></td>"; //if want delete based on staff_id echo "</tr>"; }// end while loop in delete.php file,
$id = $_get['id']; //connect db //create query based on id passed table //query : delete staff_id = $id // on success delete : redirect page original page using header() method $dbname = "your_dbname"; $conn = mysqli_connect("localhost", "usernname", "password", $dbname); // check connection if (!$conn) { die("connection failed: " . mysqli_connect_error()); } // sql delete record $sql = "delete bookings staff_id = $id"; if (mysqli_query($conn, $sql)) { mysqli_close($conn); header('location: book.php'); //if book.php main page list records exit; } else { echo "error deleting record"; }
Comments
Post a Comment