doupi6737 2016-05-16 03:37
浏览 37
已采纳

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

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" >
  • 写回答

3条回答 默认 最新

  • douteng5673 2016-05-16 04:02
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法