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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化