如何使用表格中的按钮删除特定的数据行?

时间:2016-10-29 05:35:38

标签: php mysql oop

我有一个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等创建。如何使用我的表格中包含的删除按钮删除该特定的行?

2 个答案:

答案 0 :(得分:0)

你可以试试这个

<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

答案 1 :(得分:0)

我没有在<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
}