我有一个用户类
,在其中我从我的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 等创建了该主键。我如何使用表中包含的删除按钮来删除该特定的行?
最佳答案
我没有看到任何<form>
用于 displayMyBooking()
方法,因此没有必要在那里使用单独的提交输入元素。相反,只需更改此语句
$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
访问该特定行/roomid超全局,像这样:
if(isset($_GET['roomid']) && (!empty($_GET['roomid']) || $_GET['roomid'] == 0)){
$roomid = $_GET['roomid'];
// perform delete operation
}
关于php - 如何使用表格中的按钮删除特定行的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40316357/