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

时间:2016-05-16 03:37:32

标签: php mysql arrays

我有一个表,我从mysql db填充。我想为表中的每一行添加一个删除按钮,当单击该按钮时,我想从db表中删除该行。我正在使用数组来更新对表所做的任何更改。如何使用该数组删除特定行?

<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" >

3 个答案:

答案 0 :(得分:2)

要从数据库中删除行,您需要使用带有DELETE的{​​{1}}语句,您需要从此while循环传递该语句。

在循环中建立链接:[演示]

primary key

现在在您的删除页面中,您需要使用<a href='delete.php?id=your_id'>Delete</a> 捕获或存储ID,并使用DELETE语句,您只需从数据库中删除行。

$_GET
  

注意:在删除页面中,只需查询删除行   做一些安全。

答案 1 :(得分:0)

基本上,要在没有javascript的情况下执行此操作,您需要为HTML表的每一行(显示数据库中的一行)提供单独的表单。在表单中添加一个隐藏的输入字段,其中包含该特定行的唯一标识符,以及一个用于删除该行的提交按钮。将表单操作字段留空,以便同一页面接收提交的表单数据,然后对提交的按钮进行PHP测试,如果是删除按钮,则删除数据并重新显示表格。

示例HTML代码:

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

示例PHP代码:

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

答案 2 :(得分:0)

尝试这种方法:

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

在delete.php中:

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

在edit.php中

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