简体   繁体   English

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

[英]How do i use a button in my table to delete a specific row of data?

I have a user class in which i created a table of results from my MYSQL database. 我有一个user class在其中我从MYSQL数据库创建了一个结果表。 I want to have a button on each row of the table that a user can click to delete that certain row. 我想在表格的每一行上都有一个按钮,用户可以单击该按钮以删除该特定行。

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;       
}

This function is accessed on my page simply by 该功能可以通过以下方式在我的页面上访问

       <?php
            $table = $user->displayMyBooking();
            echo $table;
        ?>

Each booking (i am developing a room booking system) has a unique composite primary key which i created with the date booked, period booked, user ID etc. How can i use the delete button included in my table to delete that specific row? 每个预订(我正在开发一个房间预订系统)都具有一个唯一的复合主键,该键由预订日期,预订期间,用户ID等创建。我如何使用表中包含的删除按钮删除该特定行?

You can try this 你可以试试这个

<button name="delete_row' value="1234">Delete</button>

or create form for each row eg 或为每一行创建表格,例如

<form action="delete.php">
<input type="hidden" name="row_id" value="1235" />
<td><input type="submit" value="Delete"></td>
<input

I don't see any <form> being used in displayMyBooking() method, so there's no point using a lone submit input element there. 我没有在displayMyBooking()方法中使用任何<form> ,因此在那里使用单独的Submit输入元素毫无意义。 Instead, simply change this statement 相反,只需更改此语句

$bookingtable .= "<td><input type='submit' name='delete' value='Delete'></td>";

to this: 对此:

$bookingtable .= "<td><a href='delete.php?roomid=" . $query->results()[$x]->roomid . "'>Delete</a></td>";

Later, on delete.php page, you can access that particular row/roomid using $_GET superglobal, like this: 稍后,在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.

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