[英]How do i use a button in my table to delete a specific row of data?
我有一個user class
在其中我從MYSQL
數據庫創建了一個結果表。 我想在表格的每一行上都有一個按鈕,用戶可以單擊該按鈕以刪除該特定行。
public function displayMyBooking() { //Added function
$userid = $this->data()->id; //gets id of user currently logged in
$query = DB::getInstance()->query("SELECT bookingdate, roomid, period FROM booking WHERE id = {$userid}"); //queries database for all bookings made with the id of the current user
$amount = $query->count(); //returns amount of results
$x=0;
//create table
$bookingtable = "<table style='width:100%' class='table'>";
$bookingtable .= "<tr>";
$bookingtable .= "<th>Room ID</th>";
$bookingtable .= "<th>Period</th>";
$bookingtable .= "<th>Booking Date</th>";
$bookingtable .= " </tr>";
for($x=0; $x<$amount; $x++) { //loop through and output data into table according to amount of results returned
$bookingtable .= "<tr>";
$bookingtable .= "<td>" . $query->results()[$x]->roomid. "</td>";
$bookingtable .= "<td>" . $query->results()[$x]->period. "</td>";
$bookingtable .= "<td>" . $query->results()[$x]->bookingdate. "</td>";
$bookingtable .= "<td><input type='submit' name='delete' value='Delete'></td>";
$bookingtable .= "</tr>";
$bookingtable .= "";
}
return $bookingtable;
}
該功能可以通過以下方式在我的頁面上訪問
<?php
$table = $user->displayMyBooking();
echo $table;
?>
每個預訂(我正在開發一個房間預訂系統)都具有一個唯一的復合主鍵,該鍵由預訂日期,預訂期間,用戶ID等創建。我如何使用表中包含的刪除按鈕刪除該特定行?
你可以試試這個
<button name="delete_row' value="1234">Delete</button>
或為每一行創建表格,例如
<form action="delete.php">
<input type="hidden" name="row_id" value="1235" />
<td><input type="submit" value="Delete"></td>
<input
我沒有在displayMyBooking()
方法中使用任何<form>
,因此在那里使用單獨的Submit輸入元素毫無意義。 相反,只需更改此語句
$bookingtable .= "<td><input type='submit' name='delete' value='Delete'></td>";
對此:
$bookingtable .= "<td><a href='delete.php?roomid=" . $query->results()[$x]->roomid . "'>Delete</a></td>";
稍后,在delete.php頁面上,您可以使用$_GET
superglobal訪問該特定行/ roomid,如下所示:
if(isset($_GET['roomid']) && (!empty($_GET['roomid']) || $_GET['roomid'] == 0)){
$roomid = $_GET['roomid'];
// perform delete operation
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.