将删除按钮添加到从数据库填充的表中

I have a table which I populate from a mysql db. I want to add a delete button to each of the rows in the table, and when the button is clicked, I want to remove that line from the db table. I am using an array to update any changes made to the table. How can I use that array to delete a specific row too?

<table>
        <tr><th>Category ID</th><th>Description</th><th>Valid</th><th></th></tr>
        <?php
        $query=mysqli_query($link,"SELECT * FROM cd_categories");
        while($row = mysqli_fetch_array($query)){
            $catid = $row['Catg_Id'];
            $des = $row['Description'];
            $datep = $row['Date_Posted'];
            $postedb = $row['Posted_By'];
            $valid = $row['Valid_YN'];
        ?>

        <tr><td><input type="text" name="data[<?php echo $catid; ?>][catid]" value="<?php echo $catid; ?>" ></td>       
        <td><input type="text" name="data[<?php echo $catid; ?>][des]" value="<?php echo $des; ?>" ></td>   
        <td><input type="button" name="data[<?php echo $catid; ?>][delete]" value="Delete" ></td>
        </tr>
        <?php } ?>
        </table>
        <br>
        <input type="submit" name="update" value="Save Changes" >

To remove a row from database you need to use a DELETE statement with a primary key, which you need to pass from this while loop.

Make a link inside while loop: [Demo]

<a href='delete.php?id=your_id'>Delete</a> 

Now in your delete page, you need to capture or store the id using $_GET and using the DELETE Statement you can simply delete row from database.

DELETE FROM table_name WHERE primary_key=your_get_value;

Note: In your delete page just make a query for delete the row also make some security.

Basically, to do this without javascript, you need to have a separate form for each row of your HTML table (which displays one row from your db). Add a hidden input field in your form which contains the unique identifier for that particular row, and a submit button to delete the row. Leave the form action field blank so the same page receives the submitted form data, then have PHP test for which button was submitted and if it was the delete button, delete the data and re-display the table.

Example HTML code:

<table>
    <tr><td> 
        <form action="" method="post">
            <input type="hidden" name="row_id" value="<?php echo 'identifier here'; ?>">
            <?php echo 'stuff here'; ?>
            <input type="submit" name="submit" value="Save Changes">
            <input type="submit" name="submit" value="Delete">
        </form>
     </td></tr>
</table>

Example PHP code:

if (isset($_POST['submit'))  // Form submitted
{
    if ($_POST['submit'] == 'Delete') // Delete button clicked
    {
        // Run delete query based on the hidden field containing the row identifier
    }
    elseif ($_POST['submit'] == 'Save Changes')
    {
        // Run update query
    }
}

Try this approach:

<table>
            <tr><th>Category ID</th><th>Description</th><th>Valid</th><th></th></tr>
            <?php
            $query=mysqli_query($link,"SELECT * FROM cd_categories");
            while($row = mysqli_fetch_array($query)){
                $catid = $row['Catg_Id'];
                $des = $row['Description'];
                $datep = $row['Date_Posted'];
                $postedb = $row['Posted_By'];
                $valid = $row['Valid_YN'];
            ?>

            <tr><td><input type="text" name="catid_<?php echo $catid; ?>" value="<?php echo $catid; ?>" ></td>       
            <td><input type="text" name="desc_<?php echo $catid; ?>" value="<?php echo $des; ?>" ></td>   
            <td>
<a href="edit.php?id=<?php echo $catid; ?>">Edit</a> |
<a href="delete.php?id=<?php echo $catid; ?>">Delete</a></td>
            </tr>
            <?php } ?>
            </table>
            <br>

in delete.php:

delete from table where cat_id= $_GET["id"];

in edit.php

$desc = 
update table set desc=$_GET["desc_".$_GET["id"]], catid = $_GET["catid_".$_GET["id"]] where cat_id= $_GET["id"];